Cut end of multiple videos

隐身守侯 提交于 2019-12-02 17:26:35

问题


I need a script to cut off the last 6 seconds of multiple videos. The videos do all have different length.

I can‘t find anything helpful online.

Does anyone know how to do that? thx


回答1:


A method that uses ffprobe to get the input duration, bc to calculate the desired output duration, and ffmpeg to perform the cutting. This method does not require the inputs to contain an audio stream, but it requires two additional tools (ffprobe and bc) instead of just ffmpeg.

Your preferred scripting language wasn't mentioned so I'll assume bash will do. In a script form as requested:

#!/bin/bash
for f in *.mp4; do
cut_duration=6
input_duration=$(ffprobe -v error -select_streams v:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 "$f")
output_duration=$(bc <<< "$input_duration"-"$cut_duration")
  ffmpeg -i "$f" -map 0 -c copy -t "$output_duration" output/"$f"
done

Or as a single line:

for f in *.mp4; do ffmpeg -i "$f" -map 0 -c copy -t "$(bc <<< "$(ffprobe -v error -select_streams v:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 "$f")"-6)" output/"$f"; done



回答2:


You have to use a roundabout way to achieve this,

ffmpeg -i file.mp4 -itsoffset 6 -i file.mp4 -c copy -map 0:a:0 -map 1 -shortest -f nut - | ffmpeg -y -i - -c copy -map 0 -map -0:0 -ss 6 trimmed.mp4

This runs two ffmpeg processes connected by a pipe, although you can do it with two ffmpeg commands run one after the another.

The first command ingests the file twice and offsets the 2nd input's timestamps by 6 seconds. It maps an audio stream from the first input and all streams from the second. The output of the first command is set to terminate with the shortest stream, which is the audio stream from the first input. The side-effect is that the last 6 seconds of the 2nd input are chopped off. In the 2nd process, all streams except the first audio stream are copied into a new container.


If you're unsure whether a file has audio, you can replace -map 0:a:0 with -map 0:0



来源:https://stackoverflow.com/questions/49964271/cut-end-of-multiple-videos

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!