0

I have tried every possible command to cut a portion (1-3 mins) of an H.264 mp4 video but it comes out good & bad. My video is 29.97 FPS, 1080p, ~80 minutes, ~3.5 GB, and at ~6200 KB/s bitrate. The problem is that my output starts off without video for 1-2 seconds then continues fine, but the audio starts off at 0 seconds. It plays well synced (audio & video) in any media player, but when using it anywhere else (rendering for production purposes) like in Adobe AE, Media Encoder, or a video converter, the FINAL rendered video starts at 0 secs where the audio started. Therefore, the audio ends up 1-2 seconds ahead and I don't want this. This also happens when trimming a YouTube video online at http://clipconverter.cc

My command is as follows:

ffmpeg -ss 01:19:22.000 -t 00:1:43.000 -i "in.mp4" -acodec copy -vcodec copy out.mp4

I even tried putting the -ss after the -i, but I get the same results. Also, I if already have a video with this issue, what can I do to fix it? What could I also do in Adobe AE for this? This site: https://ubuntuforums.org/showthread.php?t=1824250 says that it needs to be cut at the "keyframes". If so how do I do it in FFmpeg? How do I find them? What are they? I do not intend to EVER re-encode (with -c:v x264), it takes too long and recompresses the video with artifacts.

I have used both static and shared builds of FFmpeg. Also used stable 3.2.2 and ffmpeg-20170112-6596b34-win64-static, no luck.

TLDR: My video I cut in FFMPEG has missing frames (not even black or blank ones) at the beginning and results in unsynched audio/video when finally rendered in any video rendering software.

4

Use

ffmpeg -ss 01:19:22 -t 00:1:43 -i "in.mp4" -c copy -avoid_negative_ts make_zero out.mp4

Most compressed video uses temporal compression, so frames rely on other frames for full decoding. If you trim a video using copy mode, some frames after your inpoint may rely on frames before the inpoint for decoding. If so, those frames have to be included. Those frames are assigned negative timestamps so good video players use them for decoding but not for presentation. The video is shown from the inpoint onwards. Since all audio frames are keyframes, audio trimming is precise and A/V sync is preserved.

Apparently, video editors don't seem to pay heed to PTS so they show all frames stored in the file. My command above forces positive TS for all stored frames, so the video/audio will remain in sync*.

*Not completely true. For MDCT-based audio codecs like MP3/AAC, the previous frame is included with negative PTS, since it's needed for decoding. Maybe your NLE will decode that frame, leading to a 21 - 23 ms async. Workaround is to re-encode the audio.

  • Are you saying by “have to be included” that when using stream copy on video streams, and the cutting point is a non-reference frame, every preceding, needed frame within that GOP is included by ffmpeg automatically, but assigned a negative DTS? This is not what I am observing: pastebin.com/raw/fbBGxgdP – slhck Jan 17 '17 at 7:55
  • Do show_packets, not show_frames. Notice your trimmed video has no I frame. It's not missing from the file. – Gyan Jan 17 '17 at 8:08
  • Ah, I see. Didn't know -show_frames would exclude that. – slhck Jan 17 '17 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.