Answers.Body: <p>In the search of a better answer a possible way could be:</p>
  
  <pre><code>ffmpeg -i video.mkv -c copy video_fixed.mkv
  </code></pre>
  
  <p>However I'm pretty sure there are some extra options that can make the result even better..</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-16T00:03:28.877Z
Answers.Text: Works for me every time I have some kind of issue with the meta data, timestamps, or container info. And it's very fast, usually faster than 400x playspeed.
Answers.UserDisplayName: user3643
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-03T19:52:34.553Z
Answers.Text: This command saved my butt, to be frankly.
Answers.UserDisplayName: neverMind9
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-02-24T22:33:40.830Z
Answers.Text: What does it even do it looks like a copy | paste to me?
Answers.UserDisplayName: Ricardo Bohner
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-02-27T22:44:07.490Z
Answers.Text: @RicardoBohner do you mean the answer or the command? in case of the answer it was originally posted by me 1 day before the selected one, regarding the command instead it does a copy but also reading the data and checking for integrity fixes the stream.
Answers.UserDisplayName: user3450548
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-02-28T00:39:35.157Z
Answers.Text: I meant the command, and I didn't know that ffmpeg did integrity fixes when using the copy codec...thanks for clarification.
Answers.UserDisplayName: Ricardo Bohner
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-21T23:34:15.213Z
Answers.LastActivityDate: 2016-04-21T23:34:15.213Z
Answers.UserDisplayName: user3450548
Answers.Body: <p>I've tested lot of solutions and software. I love cli commands, so using ffmpeg you can fix your video file with no hassle. Try the next command if you came here searching some useful fix:</p>
  <pre><code>ffmpeg -vcodec mpeg4 -b:v 7561k -qscale:v 2 -acodec aac -ac 2 -async 1 -strict experimental ./video_fixed.mp4 -threads 0 -i damage_file.mp4
  </code></pre>
  <p>Good luck !</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-09-27T22:40:04.657Z
Answers.Text: This will probably work, but the file will be HUGE! Better so just use H264 again, like: `ffmpeg.exe -i "input.mp4" -c:v libx264 -crf 22 -preset slow output.mp4`
Answers.UserDisplayName: kett
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-11-29T17:34:22.157Z
Answers.Text: this is the only thing that worked. Can you please explain the flags? why does it work and how can it be optimized
Answers.UserDisplayName: AK_
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-12-07T15:52:19.470Z
Answers.Text: I had a video which I composed of 3 separate videos with -f concat and -safe 0, however, while the result played in VLC, in Davinci I could only see the video up to the first merge point (audio was still fine, though). After that Davinci showed a red media offline message. I first tried simply ffmpeg -c copy to fix the video but that did not work, however this re-encoding worked.
Answers.UserDisplayName: Henno
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-12-18T16:06:47.047Z
Answers.Text: I had an mp4 that didn't have a thumbnail and claimed wrong resolution in properties. Doing `ffmpeg -i input.mp4 output.mp4` didn't fix it, but converting to avi did, showing thumbnail and correct resolution. Instead of avi we can change the codec with `-c:v mpeg4` to fix it also. I tried `-c:v libx264` but didn't work since it already was that codec. So it seems the only way to fix it is to switch codec/format then switch back. Also when first converting we have to use `-q:v 0` to not lose quality. `ffmpeg -i input.mp4 -c:v mpeg4 -q:v 0 output1.mp4` then `ffmpeg -i output1.mp4 output2.mp4`.
Answers.UserDisplayName: Puddle
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-12-18T16:11:52.870Z
Answers.Text: Also if you do the avi way `ffmpeg -i input.mp4 -q:v 0 output1.avi` then `ffmpeg -i output1.avi output2.mp4`, you get the same `output2.mp4`. They generate the same md5 hash.
Answers.UserDisplayName: Puddle
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-26T11:36:52.950Z
Answers.LastActivityDate: 2020-06-26T11:36:52.950Z
Answers.UserDisplayName: user4056
Answers.Body: <p>If parts of the file reside on physically bad sectors, or for whatever reason, the OS cannot serve the whole file to FFmpeg, then naturally FFmpeg can't do anything about that. You should get a utility which can ignore those portions and write the salvageable parts to a new file, like <a href="http://www.gnu.org/software/ddrescue/ddrescue.html" rel="noreferrer">ddrescue</a>.</p>
  
  <p>Now, if the file protocol is not the cause of errors, you can try</p>
  
  <pre><code>ffmpeg -err_detect ignore_err -i video.mkv -c copy video_fixed.mkv
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-26T18:52:27.980Z
Answers.Text: This works, but fails without `-c copy`.  Surprising that it doesn't work during recode.
Answers.UserDisplayName: intuited
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-13T18:16:17.187Z
Answers.Text: Even with that, I'm still getting `Invalid data found when processing input` -- and ffmpeg gives up.
Answers.UserDisplayName: Mikhail T.
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-22T16:52:48.853Z
Answers.Text: Nice solution... if the moov atom (on a MP4 file) is still present somewhere. What if it's not even there?
Answers.UserDisplayName: Gwyneth Llewelyn
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-06T23:26:22.303Z
Answers.Text: It works at my end with surprising goodness :)
Answers.UserDisplayName: shirish
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-25T05:35:54.227Z
Answers.Text: Not worked:
  `moov atom not found
  video.mp4: Invalid data found when processing input`
Answers.UserDisplayName: 57ar7up
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-05-02T07:45:20.463Z
Answers.Text: This unfortunately consistently destroys any video file for me that I originally created with either OBS or ffmpeg
Answers.UserDisplayName: aggregate1166877
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-06-06T08:30:56.283Z
Answers.Text: @aggregate1166877 it creates a new file, so it can't destroy anything...
Answers.UserDisplayName: user924
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-06-06T11:40:07.743Z
Answers.Text: @user924 I don't mean that it deletes the old file, I mean that the new file is completely broken. English is not my first language, maybe I used phrasing not valid in English.
Answers.UserDisplayName: aggregate1166877
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-22T05:39:06.470Z
Answers.LastActivityDate: 2016-04-22T05:39:06.470Z
Answers.UserDisplayName: Gyan
Body: <p>It happened me many times that I could end up in corrupted video files:</p>
  
  <ul>
  <li>video downloaded from official sources but with torrent and for some reason not all the pieces are there in .mkv, .ts or .mp4</li>
  <li>records in .ts format where at a certain point the data is missing</li>
  <li>copied videos from an old hard drive with damaged sectors</li>
  </ul>
  
  <p>When I play this videos with VLC or other players they plays till a certain point and then instantly exits. Sometimes I can try to skip the bad part but I could meet another bad sector and have an insta-quit again.</p>
  
  <p>Question is:</p>
  
  <p>Can I fix this problems (I suppose stream and keyframes related) with ffmpeg and maybe in a -c copy fashion so I haven't to recompress everything?</p>
  
  <p>What I want to achieve is a full lenght video where the missing parts are skipped, I prefer a glitch rather than having my videos crash.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-12-01T10:40:18.977Z
Comments.Text: See also: https://superuser.com/questions/538829/repairing-corrupt-mp4
Comments.UserDisplayName: awiebe
Tags: <ffmpeg><h.264>
Title: Fix bad files and streams with ffmpeg so VLC and other players would not crash
UserDisplayName: user3450548
ThreadText: # Fix bad files and streams with ffmpeg so VLC and other players would not crash
  
  (Asked by: username_0 on 2016-04-21)
  
  It happened me many times that I could end up in corrupted video files:
  
  * video downloaded from official sources but with torrent and for some reason not all the pieces are there in .mkv, .ts or .mp4
  * records in .ts format where at a certain point the data is missing
  * copied videos from an old hard drive with damaged sectors
  
  When I play this videos with VLC or other players they plays till a certain point and then instantly exits. Sometimes I can try to skip the bad part but I could meet another bad sector and have an insta-quit again.
  
  Question is:
  
  Can I fix this problems (I suppose stream and keyframes related) with ffmpeg and maybe in a -c copy fashion so I haven't to recompress everything?
  
  What I want to achieve is a full lenght video where the missing parts are skipped, I prefer a glitch rather than having my videos crash.
  
  **Tags:** ffmpeg, h.264
  
  > **Comments:**
  > **username_1**: See also: https://superuser.com/questions/538829/repairing-corrupt-mp4  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_10 on 2016-04-22. Score: 70 | Accepted answer
  
  If parts of the file reside on physically bad sectors, or for whatever reason, the OS cannot serve the whole file to FFmpeg, then naturally FFmpeg can't do anything about that. You should get a utility which can ignore those portions and write the salvageable parts to a new file, like [ddrescue](http://www.gnu.org/software/ddrescue/ddrescue.html).
  
  Now, if the file protocol is not the cause of errors, you can try
  
  ```
  ffmpeg -err_detect ignore_err -i video.mkv -c copy video_fixed.mkv
  
  ```
  
  > **Comments:**
  > **username_11**: This works, but fails without `-c copy`.  Surprising that it doesn't work during recode.  
  > **username_12**: Even with that, I'm still getting `Invalid data found when processing input` -- and ffmpeg gives up.  
  > **username_13**: Nice solution... if the moov atom (on a MP4 file) is still present somewhere. What if it's not even there?  
  > **username_14**: It works at my end with surprising goodness :)  
  > **username_15**: Not worked: `moov atom not found video.mp4: Invalid data found when processing input`  
  > **username_16**: This unfortunately consistently destroys any video file for me that I originally created with either OBS or ffmpeg  
  > **username_17**: @username_16 it creates a new file, so it can't destroy anything...  
  > **username_16**: @username_17 I don't mean that it deletes the old file, I mean that the new file is completely broken. English is not my first language, maybe I used phrasing not valid in English.  
  
  ---
  
  ### Answer by username_0 on 2016-04-21. Score: 7
  
  In the search of a better answer a possible way could be:
  
  ```
  ffmpeg -i video.mkv -c copy video_fixed.mkv
  
  ```
  
  However I'm pretty sure there are some extra options that can make the result even better..
  
  > **Comments:**
  > **username_2**: Works for me every time I have some kind of issue with the meta data, timestamps, or container info. And it's very fast, usually faster than 400x playspeed.  
  > **username_3**: This command saved my butt, to be frankly.  
  > **username_4**: What does it even do it looks like a copy | paste to me?  
  > **username_0**: @username_4 do you mean the answer or the command? in case of the answer it was originally posted by me 1 day before the selected one, regarding the command instead it does a copy but also reading the data and checking for integrity fixes the stream.  
  > **username_4**: I meant the command, and I didn't know that ffmpeg did integrity fixes when using the copy codec...thanks for clarification.  
  
  ---
  
  ### Answer by username_5 on 2020-06-26. Score: 4
  
  I've tested lot of solutions and software. I love cli commands, so using ffmpeg you can fix your video file with no hassle. Try the next command if you came here searching some useful fix:
  
  ```
  ffmpeg -vcodec mpeg4 -b:v 7561k -qscale:v 2 -acodec aac -ac 2 -async 1 -strict experimental ./video_fixed.mp4 -threads 0 -i damage_file.mp4
  
  ```
  
  Good luck !
  
  > **Comments:**
  > **username_6**: This will probably work, but the file will be HUGE! Better so just use H264 again, like: `ffmpeg.exe -i "input.mp4" -c:v libx264 -crf 22 -preset slow output.mp4`  
  > **username_7**: this is the only thing that worked. Can you please explain the flags? why does it work and how can it be optimized  
  > **username_8**: I had a video which I composed of 3 separate videos with -f concat and -safe 0, however, while the result played in VLC, in Davinci I could only see the video up to the first merge point (audio was still fine, though). After that Davinci showed a red media offline message. I first tried simply ffmpeg -c copy to fix the video but that did not work, however this re-encoding worked.  
  > **username_9**: I had an mp4 that didn't have a thumbnail and claimed wrong resolution in properties. Doing `ffmpeg -i input.mp4 output.mp4` didn't fix it, but converting to avi did, showing thumbnail and correct resolution. Instead of avi we can change the codec with `-c:v mpeg4` to fix it also. I tried `-c:v libx264` but didn't work since it already was that codec. So it seems the only way to fix it is to switch codec/format then switch back. Also when first converting we have to use `-q:v 0` to not lose quality. `ffmpeg -i input.mp4 -c:v mpeg4 -q:v 0 output1.mp4` then `ffmpeg -i output1.mp4 output2.mp4`.  
  > **username_9**: Also if you do the avi way `ffmpeg -i input.mp4 -q:v 0 output1.avi` then `ffmpeg -i output1.avi output2.mp4`, you get the same `output2.mp4`. They generate the same md5 hash.  
  
  ---
  
  