代码之家  ›  专栏  ›  技术社区  ›  jonny

用于使用ffmpeg拆分视频的bash脚本输出错误的视频长度

  •  0
  • jonny  · 技术社区  · 6 年前

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

    其中每个部分视频的内容重叠。我这里缺什么?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Cosmin Stejerean    6 年前

    -t -to