【发布时间】:2018-11-28 14:29:03
【问题描述】:
我有一个 x 秒长的视频。我想将该视频分成相等的片段,每个片段不超过一分钟。为此,我拼凑了一个相当简单的 bash 脚本,它使用 ffmprobe 来获取视频的持续时间,找出每个片段应该有多长,然后使用 ffmpeg 迭代地分割视频:
INPUT_FILE=$1
INPUT_DURATION="$(./bin/ffprobe.exe -i "$INPUT_FILE" -show_entries format=duration -v quiet -of csv="p=0")"
NUM_SPLITS="$(perl -w -e "use POSIX; print ceil($INPUT_DURATION/60), qq{\n}")"
printf "\nVideo duration: $INPUT_DURATION; "
printf "Number of videos to output: $NUM_SPLITS; "
printf "Approximate length of each video: $(echo "$INPUT_DURATION" "$NUM_SPLITS" | awk '{print ($1 / $2)}')\n\n"
for i in `seq 1 "$NUM_SPLITS"`; do
START="$(echo "$INPUT_DURATION" "$NUM_SPLITS" "$i" | awk '{print (($1 / $2) * ($3 - 1))}')"
END="$(echo "$INPUT_DURATION" "$NUM_SPLITS" "$i" | awk '{print (($1 / $2) * $3)}')"
echo ./bin/ffmpeg.exe -v quiet -y -i "$INPUT_FILE" \
-vcodec copy -acodec copy -ss "$START" -t "$END" -sn test_${i}.mp4
./bin/ffmpeg.exe -v quiet -y -i "$INPUT_FILE" \
-vcodec copy -acodec copy -ss "$START" -t "$END" -sn test_${i}.mp4
done
printf "\ndone\n"
如果我在 30MB / 02:50 持续时间 Big Buck Bunny sample from here 上运行该脚本,则程序的输出将表明视频的长度应全部相等:
λ bash split.bash .\media\SampleVideo_1280x720_30mb.mp4
Video duration: 170.859000; Number of videos to output: 3; Approximate length of each video: 56.953
./bin/ffmpeg.exe -v quiet -y -i .\media\SampleVideo_1280x720_30mb.mp4 -vcodec copy -acodec copy -ss 0 -t 56.953 -sn test_1.mp4
./bin/ffmpeg.exe -v quiet -y -i .\media\SampleVideo_1280x720_30mb.mp4 -vcodec copy -acodec copy -ss 56.953 -t 113.906 -sn test_2.mp4
./bin/ffmpeg.exe -v quiet -y -i .\media\SampleVideo_1280x720_30mb.mp4 -vcodec copy -acodec copy -ss 113.906 -t 170.859 -sn test_3.mp4
done
由于每个部分视频的持续时间,即-ss 和-t 之间的时间,对于每个后续的ffmpeg 命令都是相等的。但我得到的持续时间更接近:
test_1.mp4 = 00:56
test_2.mp4 = 01:53
test_3.mp4 = 00:56
每个部分视频的内容重叠的地方。我在这里错过了什么?
【问题讨论】:
-
通过使用编解码器复制,您只能在关键帧上进行拆分。
-
@szatmary 我尝试删除这些参数,但问题仍然存在。有什么想法吗?