FFMPEG drops supposedly corrupt frames from original video while transcoding using h264 encoder

11,056

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
Share:
11,056

Related videos on Youtube

t6nand
Author by

t6nand

Full stack code ninja working with niche startups on very interesting technologies. Fighter, Always Optimistic and Wanderlust.

Updated on May 25, 2022

Comments

  • t6nand
    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)Corrupt Frame As seen in VLC Media Player.

    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) : Frame from video transcoded by Elastic Transcoder

    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
      Gyan almost 7 years
      I can't reproduce the behaviour with your original video.
    • t6nand
      t6nand almost 7 years
      Hi. 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
      t6nand almost 7 years
      And 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
      Gyan almost 7 years
      Run ffmpeg -i orig.mp4 -c copy orig.ts and transcode that.
    • t6nand
      t6nand almost 7 years
      Hi, 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
      Gyan almost 7 years
      Check the start time. It should be only slightly negative e.g. -0.05 at most. 0 or positive is also fine.
    • t6nand
      t6nand almost 7 years
      This 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
      Gyan almost 7 years
      No. 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
      t6nand almost 7 years
      On 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
    t6nand almost 7 years
    Although this works. But audio seem to be out of sync in final video.
  • t6nand
    t6nand almost 7 years
    Problem solved by working on syncing audio stream with video stream. Thanks.
  • d.lime
    d.lime over 3 years
    could you explain how did you sync the audio/video stream?