Answers.Body: <p>It's possible to have the black frames removed and earlier frames duplicated.</p>
  
  <pre><code>ffmpeg -i in.mp4 -vf blackframe=0,metadata=select:key=lavfi.blackframe.pblack:value=50:function=less -vsync cfr -c:a copy out.mp4
  </code></pre>
  
  <p>The blackframe filter analyses and registers the proportion of a frame's pixels which are black. The metadata filter keeps all frames which have at most 50% of pixels black. The removed frames create a gap in the video stream. So by setting the video sync method to constant frame rate, ffmpeg duplicates earlier (non-black) frames to fill in those gaps.</p>
  
  <p>You'll have to experiment with the metadata selection value <code>50</code> to get the right threshold.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-17T19:05:22.600Z
Answers.Text: This is amazing. It pretty much achieves what I wanted to (I think). I wonder though how it
  a) handles cases where there is multiple directly following black frames
  b) why the output files are consistently longer than the input files (duration wise)
Answers.UserDisplayName: Hug
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-20T09:45:57.290Z
Answers.Text: why does the output have a longer duration than the input?
Answers.UserDisplayName: Hug
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-20T10:29:10.567Z
Answers.Text: Share full log. How much difference are we talking about?
Answers.UserDisplayName: Gyan
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-26T19:34:18.380Z
Answers.Text: Hi, I'm sorry for the delay in replying. The difference is already some seconds, eg here https://pastebin.com/NfTVtEC0
  original file: https://cl.ly/qSfM
  ffmpeged file: https://cl.ly/qSMr
Answers.UserDisplayName: Hug
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-26T19:53:16.803Z
Answers.Text: Don't see a difference. Mediainfo shows format duration for original: `3 s 532 ms` and for new file `3 s 534 ms`
Answers.UserDisplayName: Gyan
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-27T10:10:57.070Z
Answers.Text: Hmm, I aligned them in Final Cut and the top one is the transcoded one and it is longer. https://cl.ly/qSQr And this is only a 3s video... And the audio lines also look different https://cl.ly/qRvs 
Answers.UserDisplayName: Hug
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-27T10:41:05.150Z
Answers.Text: Your source is variable frame rate. Apparently, FCP can't respect that - it conforms clips to constant frame rate. Which will reduce clip duration if there are frames with individual duration greater than `1/fps` seconds.
Answers.UserDisplayName: Gyan
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-09-02T22:48:12.027Z
Answers.Text: Rather than replacing with the most recent non-black frame, is it possible to have it interpolate the missing frames? \[Edit\] I've now asked [a new question for this](https://video.stackexchange.com/questions/28407/replace-all-completely-black-frames-with-interpolated-frames)
Answers.UserDisplayName: BlueRaja - Danny Pflughoeft
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-16T13:48:24.853Z
Answers.LastActivityDate: 2018-03-16T13:48:24.853Z
Answers.UserDisplayName: Gyan
Body: <p>I have problematic footage from diving due to my camera/SD card - every 4th or so frame is just black, and I want to replace all those black frames with either an interpolation between the previous and next frame or just duplicate the previous frame. </p>
  
  <p>I found this question <a href="https://video.stackexchange.com/questions/10383/remove-black-frames-from-vob-files-with-ffmpeg">"remove black frames from vob files with ffmpeg"</a>, but I don't want to cut out the frames but instead want to interpolate between the two frames or duplicate the previous frame. </p>
  
  <p>I think I might be able to get all the relevant positions with the black-frame filters etc, but what kind of actions would I use to replace those frames with an interpolation or a duplicate of the previous frame in a fully automatic way (so I only pass my movie file into the script)? </p>
  
  <p>All I've found so far is the "shuffleframes" method, which I could somehow (how?) feed with the position of the black frames and then thereby duplicate the previous frame.</p>
  
  <p>Demo footage detailing the probem can be found <a href="https://cl.ly/qCr1" rel="nofollow noreferrer">here</a>.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-09-02T22:49:40.900Z
Comments.Text: Highly related: https://video.stackexchange.com/questions/13423/any-nice-tool-for-replacing-all-black-frames-in-a-video-with-another-frame https://video.stackexchange.com/questions/28407/replace-all-completely-black-frames-with-interpolated-frames
Comments.UserDisplayName: BlueRaja - Danny Pflughoeft
Tags: <ffmpeg><filter><blackmagic>
Title: replace black frames with previous frames
UserDisplayName: Hug
ThreadText: # replace black frames with previous frames
  
  (Asked by: username_0 on 2018-03-15)
  
  I have problematic footage from diving due to my camera/SD card - every 4th or so frame is just black, and I want to replace all those black frames with either an interpolation between the previous and next frame or just duplicate the previous frame.
  
  I found this question ["remove black frames from vob files with ffmpeg"](https://video.stackexchange.com/questions/10383/remove-black-frames-from-vob-files-with-ffmpeg), but I don't want to cut out the frames but instead want to interpolate between the two frames or duplicate the previous frame.
  
  I think I might be able to get all the relevant positions with the black-frame filters etc, but what kind of actions would I use to replace those frames with an interpolation or a duplicate of the previous frame in a fully automatic way (so I only pass my movie file into the script)?
  
  All I've found so far is the "shuffleframes" method, which I could somehow (how?) feed with the position of the black frames and then thereby duplicate the previous frame.
  
  Demo footage detailing the probem can be found [here](https://cl.ly/qCr1).
  
  **Tags:** ffmpeg, filter, blackmagic
  
  > **Comments:**
  > **username_1**: Highly related: https://video.stackexchange.com/questions/13423/any-nice-tool-for-replacing-all-black-frames-in-a-video-with-another-frame https://video.stackexchange.com/questions/28407/replace-all-completely-black-frames-with-interpolated-frames  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2018-03-16. Score: 6 | Accepted answer
  
  It's possible to have the black frames removed and earlier frames duplicated.
  
  ```
  ffmpeg -i in.mp4 -vf blackframe=0,metadata=select:key=lavfi.blackframe.pblack:value=50:function=less -vsync cfr -c:a copy out.mp4
  
  ```
  
  The blackframe filter analyses and registers the proportion of a frame's pixels which are black. The metadata filter keeps all frames which have at most 50% of pixels black. The removed frames create a gap in the video stream. So by setting the video sync method to constant frame rate, ffmpeg duplicates earlier (non-black) frames to fill in those gaps.
  
  You'll have to experiment with the metadata selection value `50` to get the right threshold.
  
  > **Comments:**
  > **username_0**: This is amazing. It pretty much achieves what I wanted to (I think). I wonder though how it a) handles cases where there is multiple directly following black frames b) why the output files are consistently longer than the input files (duration wise)  
  > **username_0**: why does the output have a longer duration than the input?  
  > **username_2**: Share full log. How much difference are we talking about?  
  > **username_0**: Hi, I'm sorry for the delay in replying. The difference is already some seconds, eg here https://pastebin.com/NfTVtEC0 original file: https://cl.ly/qSfM ffmpeged file: https://cl.ly/qSMr  
  > **username_2**: Don't see a difference. Mediainfo shows format duration for original: `3 s 532 ms` and for new file `3 s 534 ms`  
  > **username_0**: Hmm, I aligned them in Final Cut and the top one is the transcoded one and it is longer. https://cl.ly/qSQr And this is only a 3s video... And the audio lines also look different https://cl.ly/qRvs   
  > **username_2**: Your source is variable frame rate. Apparently, FCP can't respect that - it conforms clips to constant frame rate. Which will reduce clip duration if there are frames with individual duration greater than `1/fps` seconds.  
  > **username_1**: Rather than replacing with the most recent non-black frame, is it possible to have it interpolate the missing frames? \[Edit\] I've now asked [a new question for this](https://video.stackexchange.com/questions/28407/replace-all-completely-black-frames-with-interpolated-frames)  
  
  ---
  
  