
기본적으로 저는 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