【问题标题】:Bash script for splitting video using ffmpeg outputs wrong video lengths使用 ffmpeg 分割视频的 Bash 脚本输出错误的视频长度
【发布时间】: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 我尝试删除这些参数,但问题仍然存在。有什么想法吗?

标签: windows bash video ffmpeg


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2015-05-07
    • 2017-05-25
    • 2016-09-08
    • 2019-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多