Tech Blog

Convert FLV to MP4 with ffmpeg Howto

Posted At : December 29, 2010 5:13 AM 5 Comments

I recently needed to convert a lot of FLV files which were H264 encoded to MP4 so that they would play nice on my Mac Mini.

Initial attempts using ffmpeg were making it re-encode the entire video which would take ages and result in a larger file or worse quality.

A bit of googling and reading the man pages later I discovered

-vcodec copy -acodec copy

This tells ffmpeg to copy the video and audio without re-encoding.

So insted of this

ffmpeg -i input.flv output.mp4

The Solution

ffmpeg -i input.flv -vcodec copy -acodec copy output.mp4

Further complications

This worked a treat for all except one file - which gave the following error.

[NULL @ 0x9b6b9f0]error, non monotone timestamps 37464141 >= 37463126
av_interleaved_write_frame(): Error while opening file

By using the "-an" and "-vn" flags to skip the video and audio encoding in turn I narrowed it down to a problem in the audio codec timestream.

To try to get ffmpeg to fix the problem I got it to reencode the audio but copy the video codec with the following:

ffmpeg -i input.flv -vcodec copy -acodec mp2 -ar 44100 -ab 128k output.mp4

Worked a treat. I love ffmpeg :-)

Update

As usually happens, by the time I finished writing the post I spotted another enhancement - using the libfaac codec (I had been trying to use it as aac which was failing).

Final code to fix it using aac with audio quality quite high 200 (range is 0-255)

ffmpeg -i input.flv -vcodec copy -acodec libfaac -aq 200 output.mp4

5 Comments

Rob Shaw 12/29/10 9:03 AM

check out handbrakeCLI (ffmpeg + lots more via the command line). http://handbrake.fr/downloads2.php

I presented about video at cfobjective anz this yr, here are my slides:-

http://slidesix.com/view/Embracing-Media-in-your-C...

Mark Lynch 12/29/10 12:15 PM

Hi Rob, Good to hear from you. Yep, I use handbrake quite a bit - but prefer to it with the low level tools as much as I can :-) Is it possible to make handbrake not re-encode?

Cheers,
Mark

Tim 8/30/11 10:31 AM

Incredibly helpful post: Thank you! What was taking around half my recording time, is now running at over 100 x real time!

Blayne 10/17/11 8:40 PM

Thanks for the writeup.

How would you write this to process all the .flv files in a certain directory? - perhaps recursively as well...

Joel 10/19/11 4:44 PM

@Blayne:
for file in `ls *.flv`; do ffmpeg .... ; done

this will work in your current directory... you could also do:
find ./ -type f -name *.flv -exec ffmpeg ... {} \;
just test them out with simple echo's instead of ffmpeg so you can familiarize yourself with the build in commands.