基本上,我尝试编辑一个 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