FFMPEG drops supposedly corrupt frames from original video while transcoding using h264 encoder
Your source video has some frames without timestamps.
In this particular case, extracting to raw bitstream and then transcoding works:
ffmpeg -i orig.mp4 -c copy orig.264
ffmpeg -i orig.264 -i orig.mp4 -map 0 -map 1:a -c:v libx264 -crf 25 -vf scale="-2:min(ih\,720)" -b:v 600K -g 90 -c:a libfdk_aac output.mp4
Related videos on Youtube
![t6nand](https://i.stack.imgur.com/R0sLG.jpg?s=256&g=1)
t6nand
Full stack code ninja working with niche startups on very interesting technologies. Fighter, Always Optimistic and Wanderlust.
Updated on May 25, 2022Comments
-
t6nand about 2 years
I am using h264 for optimising MP4 for web. I have a video which has some supposedly corrupt frame(s) within it's initial 1-2 seconds. (Frame provided below)
On transcoding using :
ffmpeg -i orig.mp4 -c:v libx264 -crf 25 -vf scale="-2:min(ih\,720)" -b:v 600K -g 90 -c:a libfdk_aac output.mp4
The output MP4 has these frames dropped out and I have my output with it's start about 1 to 2 seconds delayed from original video, thus resulting in overall less time duration in output video.
Moreover, most media players also skip these frames in playback (like quicktime player, etc). But VLC media player was able to play this video without skipping these frames.
Is there a way to not drop frames using ffmpeg? And if possible is it possible to identify and fix these frames in a video?
Note: I tried encoding same video using AWS Elastic transcoder which actually fixed these frames (Frame provided below) :
Note: Original video can be found here - https://drive.google.com/file/d/0B9VkhR9Zu60ybXFDeno3RGpQTUE/view?usp=sharing Video transcoded by AWS Elsatic transcoder can be found here - https://drive.google.com/file/d/0B9VkhR9Zu60yWUVHQk5MTk05QVk/view?usp=sharing
EDIT1: As suggested by @Mulvya in comments, TS-transcoded video can be found here - (https://drive.google.com/file/d/0B9VkhR9Zu60yU0t6T0dMME9ZMmc/view?usp=sharing)
-
Gyan almost 7 yearsI can't reproduce the behaviour with your original video.
-
t6nand almost 7 yearsHi. Try opening original video using VLC media player. You will be able to observe corrupt frame within 1st 1 to 2 seconds. Also Take a look at this video transcoded by AWS elastic transcoder - (drive.google.com/file/d/0B9VkhR9Zu60yWUVHQk5MTk05QVk/…) . You will clearly see the difference in initial frames and video time duration. This transcoding was done using original video provided in question.
-
t6nand almost 7 yearsAnd just to add if it helps to reproduce the issue, I tried extracting every frame from original video both using ffmpeg and vlc media player. Ffmpeg started it's extraction after these corrupted frames. However, using video scene filter on VLC to extract frames I was able to get these corrupted frames as one shown in question.
-
Gyan almost 7 yearsRun
ffmpeg -i orig.mp4 -c copy orig.ts
and transcode that. -
t6nand almost 7 yearsHi, Thanks. It worked and transcoded videos were correct. But is it possible to detect such issues in videos firsthand and only run this command when there is some problem.
-
Gyan almost 7 yearsCheck the start time. It should be only slightly negative e.g.
-0.05
at most.0
or positive is also fine. -
t6nand almost 7 yearsThis method appears to copy first frame of video in initial 1-2 seconds. It's not exactly what I want. Checked it by extracting frames and first 18 frames were 1st frame copied. Can you confirm this?
-
Gyan almost 7 yearsNo. Don't rely on frame extraction to check. FFmpeg defaults to CFR emulation unless set otherwise, so it will duplicate frames. Check the first few frames of aws and TS-transcode in a player.
-
t6nand almost 7 yearsOn comparing AWS and TS-transcoded videos for first few frames in a player there are visible differences. AWS transcoded video appears to have fixed all corrupted frames, but in TS-transcoded video there was duplication of frame as you explained earlier. Here is TS-transcoded video for reference (drive.google.com/file/d/0B9VkhR9Zu60yU0t6T0dMME9ZMmc/…)
-
-
t6nand almost 7 yearsAlthough this works. But audio seem to be out of sync in final video.
-
t6nand almost 7 yearsProblem solved by working on syncing audio stream with video stream. Thanks.
-
d.lime over 3 yearscould you explain how did you sync the audio/video stream?