需要 MPV bash 腳本的幫助

需要 MPV bash 腳本的幫助

基本上我正在嘗試編輯 bash 腳本。該腳本的目標是允許我透過從終端啟動它來拍攝一堆視訊螢幕截圖,只需指定檔案名稱和所需的螢幕截圖數量。我已經解決了一些錯誤,但在讓 MPV 分配我想要的檔案名稱時遇到問題。

這是當前的腳本。

#!/bin/bash


### Global variables
filename="$1"

### Error handling
if [ -z ${filename} ]; 
then
    echo "ERROR: No video file supplied. Please enter a video file as argument."
    exit 1;
fi

NUM_OF_SCREENSHOTS=9
if [ ! -z $2 ]; 
then
  NUM_OF_SCREENSHOTS=$2
  echo "WARNING: Overwrite default number of screenshots to ${NUM_OF_SCREENSHOTS}."
  sleep 3s
fi




# Get the total length of the video in seconds.
#  Use mplayer to display the info of the video and then get the value of ID_LENGTH, the total number of seconds of the video.
total_length=$(mplayer -identify -frames 0 -vc null -vo null -ao null "$filename" | grep ID_LENGTH | sed 's/ID_LENGTH=//' | sed 's/\..*//')
# Reference https://github.com/mpv-player/mpv/blob/master/TOOLS/mpv_identify.sh

# Remove 4 seconds from the video so that it doesn't take screenshot at the ends.
let total_length-=4

# time_slice: At which time interval should mplayer take screenshots.
let time_slice=${total_length}/${NUM_OF_SCREENSHOTS}

# time_at: When should mplayer take screenshots.
time_at=${time_slice};

# Looping to take screenshots.
for ((i=1; i <= NUM_OF_SCREENSHOTS ; i++))
do


  # Take the screenshot.
  #mplayer -loop 1 -nosound -frames 1 -ss ${time_at} -vo png:z=9 ${filename}
  mpv --quiet --no-audio --vo=image --screenshot-template="%f %n" --start=${time_at} --frames=1 "$filename" 

  # Increment to the next time slice.
  let time_at+=${time_slice}



done

答案1

該腳本無法以這種方式運行。該腳本大部分都很好,儘管您應該進行一些小的改進(使用外殼檢查為了那個原因。

問題是,mpv使用內部計數器來命名螢幕截圖檔案 ( %n)。並且計數器在每次循環時都會重置。

下面修改後的腳本重命名該檔案。

#!/usr/bin/env bash

### Global variables
filename="$1"

### Error handling
if [ -z "${filename}" ]; 
then
    echo "ERROR: No video file supplied. Please enter a video file as argument."
    exit 1;
fi

NUM_OF_SCREENSHOTS=9
if [ ! -z "$2" ]; 
then
  NUM_OF_SCREENSHOTS=$2
  echo "WARNING: Overwrite default number of screenshots to ${NUM_OF_SCREENSHOTS}."
  sleep 3s
fi

# Get the total length of the video in seconds.
#  Use mplayer to display the info of the video and then get the value of ID_LENGTH, the total number of seconds of the video.
total_length=$(mplayer -identify -frames 0 -vc null -vo null -ao null "$filename" | grep ID_LENGTH | sed 's/ID_LENGTH=//' | sed 's/\..*//')
# Reference https://github.com/mpv-player/mpv/blob/master/TOOLS/mpv_identify.sh

# Remove 4 seconds from the video so that it doesn't take screenshot at the ends.
let total_length-=4

# time_slice: At which time interval should mplayer take screenshots.
let time_slice=${total_length}/${NUM_OF_SCREENSHOTS}

# time_at: When should mplayer take screenshots.
time_at=${time_slice};

# Looping to take screenshots.
for ((i=1; i <= NUM_OF_SCREENSHOTS ; i++))
do

  # Take the screenshot.
  #mplayer -loop 1 -nosound -frames 1 -ss ${time_at} -vo png:z=9 ${filename}
  mpv --quiet --no-audio --vo=image --start=${time_at} --frames=1 "$filename" 
  rename 's/^[0-9]+/out'"${time_at}"'/' 00000001.jpg

  # Increment to the next time slice.
  let time_at+=${time_slice}

done

exit 0

相關內容