简体   繁体   中英

I would like to batch-trim mp4 videos in a folder with command line (ffmpeg?), however the sound is delayed

I found the following command line to batch-trim videos in a folder, however it delays the sound of all the videos by a few seconds (sound comes after the video):

for file in /path/to/folder/*.mp4; do ffmpeg -i "$file" -ss 00:00:08 -c copy -avoid_negative_ts 1 "${file%.*}_trimmed.mp4"; done

These are the alternatives I found, however none of them solve the problem:

for file in /path/to/folder/*.mp4; do ffmpeg -i "$file" -itsoffset -0.5 -ss 8 -c:v copy -c:a copy "${file%.*}_trimmed.mp4"; done

for file in /path/to/folder/*.mp4; do ffmpeg -i "$file" -ss 8 -async 1 -c:v copy -c:a copy "${file%.*}_trimmed.mp4"; done

for file in /path/to/folder/*.mp4; do ffmpeg -i "$file" -ss 8 -map 0:v -map 0:a -c:v copy -c:a copy "${file%.*}_trimmed.mp4"; done

I have a hard time finding by how much time the sound is delayed.

My questions:

  • what would be the command line to batch-trim the beginning of all the videos in a folder with the sound properly "aligned" to the video?
  • in other words, can't the sound be by default trimmed by as much as the video in the first place? If yes, how?

System used:

  • macOS Ventura (13.1, Intel)
  • Shell and version: zsh 5.8.1 (x86_64-apple-darwin22.0)
  • ffmpeg version 5.0 built with Apple clang version 13.0.0 (clang-1300.0.29.30)

from the FFMPEG(1) manual page:

 -ss position (input/output) When used as an input option (before "-i"), seeks in this input file to position. Note that in most formats it is not possible to seek exactly, so ffmpeg will seek to the closest seek point before position. When transcoding and -accurate_seek is enabled (the default), this extra segment between the seek point and position will be decoded and discarded. When doing stream copy or when -noaccurate_seek is used, it will be preserved. When used as an output option (before an output url), decodes but discards input until the timestamps reach position. position must be a time duration specification, see the Time duration section in the ffmpeg-utils(1) manual.

(quoted under license, For details about the authorship, see the Git history of the project (git://source.ffmpeg.org/ffmpeg), eg by typing the command git log in the FFmpeg source directory, or browsing the online repository at http://source.ffmpeg.org .)

So first of all I would not be astonished that something is out of sync as it is documented and also reasonable.

As you're doing a stream copy (and not transcoding) the "overlap" will be preserved and from your description this leads to the async.

If you can easily reproduce with a single file I'd try to tweak with command line switch(es). Eg if accurate seek can be used with copy as you perhaps don't want to re-encode and if it helps with the sync already. I'd still keep it in a file so that I can have it under version control.

This also might depend on the seek points in the original material, therefore I'd also test if re-encoding (transcoding) can help and with which options. Getting results with that can help to decide on the concrete material which way is to be favored.

When that has been decided, increase the batch size and check the output if its still as expected as with the smaller test. Then rinse and repeat until you get your wanted results.

The zsh shell scripting is helping you to formulate the command as they can become complex and you can write them down in a file. It also makes it easier for batch handling. Strictly speaking about what you ask for, this is purely about ffmpeg and technically the zsh shell scripting is unrelated to it. Just saying so it is easier to keep different issue apart (if you're later on missing files, it is likely a flaw in the scripting or error handling in the script for example).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM