• Skip to content
  • Skip to primary sidebar
  • Skip to footer

Foliovision

Main navigation

  • Weblog
    • FV Player
    • WordPress
    • Video of the Week
    • Case Studies
    • Business
  • About
    • Testimonials
    • Meet the Team
    • We Support
    • Careers
    • Contact
    • Pricing
  • Products
  • Support
    • FV Player Docs
    • Pro Support
  • Login
  • Basket is empty
Affordable VAST/VPAID for Wordpress has arrived. Serve ads with your videos starting today!

How to repair broken mp4, m4v and mkv videos with ffmpeg

3 September 2021 / Alec Kinnear / Leave a Comment

Short version for trained commandos

Open terminal and navigate to the directory where the broken video files are stored. Run this command in terminal:

for f in *.mp4; do ffmpeg -i "${f}" -c copy ${f%%.*}-2.mp4; done

Full version including backstory with instructions on proper installation and use of ffmpeg on OS X

I’ve been using a combination of MDRP (MacDVDRipper Pro) and Submerge to create hard subtitled versions of Austrian classic show Kommissar Rex for our six year old starting school this year in German. We don’t speak German at home, so it’s important that most of his entertainment comes in this language. I need the subtitles to be able to follow the action closely and help Constantin with vocabulary.

The finished files play and the subtitles look great:

Kommissar Rex S01E02 with hard subtitles added in Submerge
play-sharp-fill

Kommissar Rex S01E02 with hard subtitles added in Submerge

Unfortunately on the computer the container is broken and the video shows as being 1192:52:46 long. That’s 1192 hours long.

Screenshot of very long timecode-after-Submerge ffmpeg conversion.

Screenshot of very long timecode-after-Submerge ffmpeg conversion.
A 45 minute episode shows as 1192 hours long

The 1192 hour length makes it impossible to use the timeline to scrub (right away one is in blackness). It’s not a critical issue – the files do play back on our Sony X700 and our Pioneer BDP-450 blu-ray players – but I don’t like broken things so I set out to find out what the issue is.

First I tried using some of the other Submerge presets to create my submasters with hard subtitles. No luck, they all showed 11.59.59.45 as the movie length (is that one week or sixteen hours). Next I tried modifying the presets in Submerge (located at within the app package at Submerge.app/Contents/Resources/Presets but didn’t have any luck changing them enough to be able to create movie containers which weren’t three days long.

Subtitles created from the same MDRP masters in ffWorks don’t have this issue – the length is normal, 45 minutes. Files created from Submerge hard-coded subtitle submasters with 1192 hour time codes after a second conversion in ffWorks also show the correct time code.

How ffmpeg can fix a broken .mp4 and .mkv video file almost instantly

Experimenting with the Submerge ffmpeg preset files made me think more about using standalone ffmpeg (the engine in both ffWorks and Submerge) to clean up these files. I remembered hearing about a zero copy which could make broken mp4’s downloaded from the internet open and play again. It took awhile to find the code to do this but eventually I did.

A simple repackaging of the file is this command:

/Users/Alec/Library/Application\ Support/FFmpegTools/ffmpeg -i input.mp4 -c copy output.mp4

What’s great about this command is that it’s almost instantaneous (400x real time speed) and does not re-encode so your output video remains the same generation as the input.

Fixing more problematic files with ffmpeg

There’s a more sophisticated version of the command above which performs a basic transcoding.

/Users/Alec/Library/Application\ Support/FFmpegTools/ffmpeg -i "input.mp4" -c:v libx264 -crf 22 -preset slow output.mp4

The benefit of re-encoding the file is that it will definitively remove any encoding errors. The downside is that it ages your video another generation. It’s basically like resaving a jpeg image. Gradually blocky shadows, chroma noise and jaggies will get worse.

You can choose whichever ffmpeg library you like on your computer. To start this experiment is just used the most recent of the dozens of copies I have on this computer rather than reinstalling. Now that I’m using ffmpeg directly from the command line, I’d like to be able to run the ffmpeg binary without a path. Installing ffmpeg on OS X used to require install the homebrew framework first. Happily not any more.

How to install ffmpeg without path on OS X

To do so, one should add yet another copy of ffmpeg in usr/local/bin. Here’s how.

  1. download ffmpeg and decompress the zip
  2. in Finder use the shift-command-G (Go to Folder) com to open the otherwise hidden /usr/local/bin directory
  3. drag and drop the ffmpeg binary there (OS X will ask for authorisation as /usr/local/bin is a protected folder
  4. chmod, as sudo, the files to make them executable: sudo chmod +x /usr/local/bin/ff*
  5. type ffmpeg into terminal to make sure all is working.

The output should look like this:

OfficeTower:~ alec$ ffmpeg
ffmpeg version 3.4.2 Copyright (c) 2000-2018 the FFmpeg developers
  built with Apple LLVM version 8.0.0 (clang-800.0.42.1)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/3.4.2 --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --disable-jack --enable-gpl --enable-libmp3lame --enable-libx264 --enable-libxvid --enable-opencl --enable-videotoolbox --disable-lzma
  libavutil      55. 78.100 / 55. 78.100
  libavcodec     57.107.100 / 57.107.100
  libavformat    57. 83.100 / 57. 83.100
  libavdevice    57. 10.100 / 57. 10.100
  libavfilter     6.107.100 /  6.107.100
  libavresample   3.  7.  0 /  3.  7.  0
  libswscale      4.  8.100 /  4.  8.100
  libswresample   2.  9.100 /  2.  9.100
  libpostproc    54.  7.100 / 54.  7.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

Use -h to get full help or, even better, run 'man ffmpeg'

In my case, I didn’t need step four (chmod) for ffmpeg to load and work. More detailed installation instructions here.

How to convert not single video files but whole directories with ffmpeg

The next issue I faced is that I have directory full of thirty of these files.

Writing out /Users/Alec/Library/Application\ Support/FFmpegTools/ffmpeg -i inputvideo.mp4 -c copy /path/to/outputvideo.mp4 thirty times didn’t sound like much fun, even when copying and pasting paths. There’s lots of ways to automate a directory conversion on OS X, via Automator or AppleScript of KeyboardMaestro.

But since we are already on the command line, doing the whole batch conversion in bash makes the most sense. The answers linked at Stackoverflow are mostly too complicated with multiple line commands. It can all be condensed down to a single line, which is easier to read:

Here’s how:

for f in *.mp4; do ffmpeg -i "${f}" -c copy ${f%%.*}-2.mp4; done

This will create a second version of every video with “-2” at the end of the filename: yourvideo.mp4 –> yourvideo-2.mp4

Or alternatively, to put the files in a subdirectory:

for f in *.mp4; do ffmpeg -i "${f}" -c copy "copies/${f}"; done

Don’t forget to create the “copies” subdirectory first though or the command will fail.

How to convert .mp4 files to .mkv format with ffmpeg

The copy command also works to convert an mp4 to a mkv file:

for f in *.mp4; do ffmpeg -i "${f}" -c copy "${f%%.*}.mkv"; done

In this case no subdirectory or number is needed as the the .mkv files will not overwrite the .mp4 originals (better make sure it’s not a mixed directory with .mp4 in that case). For a mixed directory of course one should run:

for f in *.mp4; do ffmpeg -i "${f}" -c copy "copies/${f%%.*}.mkv"; done

With both of these single line commands, it is so easy and fast to clean up whole directories of .mkv and .mp4 video files that I’ll be using ffmpeg far more often from the command line. Remember that by using this command, you are not re-encoding the video but just letting ffmpeg clean up and put the file in a fresh box.

How to convert .mp4 files to .m4v format with ffmpeg

The copy command also works to convert an mp4 to a m4v file:

for f in *.mp4; do ffmpeg -i "${f}" -c copy "${f%%.*}.m4v"; done

How to convert .mp4 files to .webm format with ffmpeg

Recent versions of webm allow us to convert to Google’s webm format.

ffmpeg -i input.mov -c:v libvpx -preset slow output.webm

In this case no subdirectory or number is needed as the the .webm files will not overwrite the .mp4 originals (better make sure it’s not a mixed directory with .webm in that case).

Encoding presets for creating .mp4 submasters with ffmpeg

This same command structure can be used for encoding in other formats, i.e. the main conversion. A set of good presets for creating web versions of videos is the subject for another article as our current instructions for encoding HTML 5 videos could use an update.

Here’s a basic conversion though:

ffmpeg -i "input.mov" -c:v libx264 -crf 20 -preset slow output.mp4

This will convert the master to .mp4 format with very high quality at the existing resolution.

  • -crf 20 controls visual quality and file size: from 17 to 28 are sane values. Feel free to experiment with file size to suit the bandwidth of your server and the importance of quality.
  • -preset slow affects encoding speed and degree of compression. Slow is the optimum setting for good visual quality and fast conversion. There are also -slower and veryslow. slower is about twice as slow with no visible difference (less than 3% tested with tools). veryslow is more than three times as slow with no visible difference (less than 1% tested with tools) than slow.
ffmpeg speed encoding test chart
corbpie.com speed tests

Happy encoding!

Alec Kinnear

Alec Kinnear

Alec has been helping businesses succeed online since 2000. Alec is an SEM expert with a background in advertising, as a former Head of Television for Grey Moscow and Senior Television Producer for Bates, Saatchi and Saatchi Russia.

Categories: FV Player, IT, WordPress Tags: encoding, ffmpeg, video

Related Posts

  1. How to change path to ffmpeg?

    How to change path to ffmpeg?

  2. How to update your WordPress plugins from a local copy on Mac

    How to update your WordPress plugins from a local copy on Mac

  3. How to Use Multiple Audio Tracks with FV Player

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You can click here to Subscribe without commenting

Primary Sidebar

Categories

  • Business
  • Camera Reviews
  • Case Studies
  • Design
  • FV Player
  • Internet Marketing
  • IT
  • Life
  • SEO
  • Slovak
  • Video of the Week
  • WordPress

Footer

Our Plugins

  • FV WordPress Flowplayer
  • FV Thoughtful Comments
  • FV Simpler SEO
  • FV Antispam
  • FV Gravatar Cache
  • FV Testimonials

Free Tools

  • Pandoc Online
  • Article spinner
  • WordPress Password Finder
  • Delete LinkedIn Account
  • Responsive Design Calculator
Foliovision logo
All materials © 2025 Foliovision s.r.o. | Panská 12 - 81101 Bratislava - Slovakia | info@foliovision.com
  • This Site Uses Cookies
  • Privacy Policy
  • Terms of Service
  • Site Map
  • Contact
  • Tel. ‭+421 2/5292 0086‬

We are using cookies to give you the best experience on our website.

You can find out more about which cookies we are using or switch them off in .

Powered by  GDPR Cookie Compliance
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Necessary Cookies

Strictly Necessary Cookie allow you to log in and download your software or post to forums.

We use the WordPress login cookie and the session cookie.

If you disable this cookie, we will not be able to save your preferences. This means that every time you visit this website you will need to enable or disable cookies again.

Support Cookies

Foliovision.com uses self-hosted Rocket.chat and self-hosted Freescout support desk to provide support for FV Player users. These cookies allow our visitors to chat with us and/or submit support tickets.

We are delighted to recommend self-hosted Rocket.chat and especially Freescout to other privacy-conscious independent publishers who would prefer to self-host support.

Please enable Strictly Necessary Cookies first so that we can save your preferences!

3rd Party Cookies

This website uses Google Analytics and Statcounter to collect anonymous information such as the number of visitors to the site, and the most popular pages.

Keeping this cookie enabled helps us to improve our website.

We reluctantly use Google Analytics as it helps us to test FV Player against popular Google Analytics features. Feel free to turn off these cookies if they make you feel uncomfortable.

Statcounter is an independent Irish stats service which we have been using since the beginning of recorded time, sixteen years ago.

Please enable Strictly Necessary Cookies first so that we can save your preferences!