gpt4 book ai didi

windows - 使用 ffmpeg 分割视频的 Bash 脚本输出错误的视频长度

转载 作者:行者123 更新时间:2023-12-04 22:59:31 25 4
gpt4 key购买 nike

我有一个视频是 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

每个部分视频的内容重叠的地方。我在这里想念什么?

最佳答案

您似乎在使用 -t ,这是复制的持续时间,但您将其传递到结束位置。也许您打算使用 -to哪个是结束位置?或者您可以使用 -t但是您需要计算从开始到结束的持续时间。

也就是说,有更好的方法可以将视频分成长度大致相等的片段,您应该查看片段复用器。

为了保证一定持续时间的片段,您很可能还需要对视频进行转码并以固定间隔引入关键帧,否则您的分割将在最近的关键帧上,而不是您想要的确切持续时间。

关于windows - 使用 ffmpeg 分割视频的 Bash 脚本输出错误的视频长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50931556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com