Using FFMpeg to Convert mpg or mov to flv

If you need to use a command-line program to convert between different movie or media formats, it’s likely you’ll end up using ffmpeg. ffmpeg is the de facto standard for converting file formats in *nix and FreeBSD environments.

Using the program from the command-line is like using any other program. There are a large amount of switches and options you can use to change the functionality of ffmpeg, including video resolution, video quality, audio compression and quality, framerate, and more!

There are many ways to download ffmpeg. Many vary on your distribution of Linux or other Operating System. In many cases, ffmpeg may already be installed and ready for you to use. For Mandriva users, ffmpeg is available using urpmi or the “Add/Remove Software” program in the control panel.

In case you simply need to convert a video from mov or mpeg format to Flash Video (flv) here is the command that I use:

# ffmpeg -i <filename.mpg> -deinterlace -ar 44100 -r 25 -qmin 3 -qmax 6 <filename.flv>

This will convert your movie to Flash video (.flv extension) at the same resolution it went in as (for example 480×392 pixels in width and height), deinterlace the video, set the audio frequency to 44100 (high quality), the video framerate to be 25 frames per second, and set the video quality between 3 and 6, which will give you very good yet quickly servable results. If you’re shipping this video on CD or DVD you can set the qmin and qmax values lower (lower = higher quality) but for streaming video this is very, very good and very close to the original without being massive in size.

In fact, the one thing that requires some explanation are the qmin and qmax values. It’s a slightly complicated subject but can be easily explained by thinking of qmin and qmax as how much quality you want to take away from your video, between those two numbers. The minimum qmin and qmax is 1 and the maximum is 31.

If the video size is too large after using this command example and you would like a smaller file, try increasing qmax first until you reach it’s maximum. It’s likely that you will find a happy number in there without having to adjust the qmin value.

If you need to convert several videos, say from a directory, you can use this script:

<?php
if ($handle = opendir('.')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != ".." && strpos($file, "mpg") !== false) {
                $new_filename = str_replace(" ", "_", strtolower(str_replace("mpg", "flv", $file)));
                $file = str_replace(" ", "\ ", $file);
                exec("ffmpeg -i $file -deinterlace -ar 44100 -r 25 -qmin 3 -qmax 6 $new_filename");
        }
    }
    closedir($handle);
}
?>

It’s fairly self-explanatory, but the above script simply fetches a list of all of the files in the current directory and executes the ffmpeg program for each file in that list that has the extension “.mpg”.

It should be noted that converting between different video file formats may require a license to do so by the patent holders of that file format. Be sure that you have dotted your “i’s” and crossed your “t’s”.

21 thoughts on “Using FFMpeg to Convert mpg or mov to flv

  1. Hi John,

    Am I right in assuming that if I set qmin=1 and qmax=2 that I will get the largest filesize (best quality), and qmin=30, qmax=31 => small size, poor quality? (qmin=1, qmax=31 something in between?)

    So q being some kind of quality reduction allowance?

  2. Magnus,

    That’s entirely correct. The easiest way to start chipping away at the filesize is to set qmin to 1 and slightly move qmax up until you hit 31. Then you put qmin to 2 and reset qmax, slowly moving it back up to 31 again each time until you’re happy with the file size and quality.

    The higher the numbers, the smaller (and lower-quality!) your video will be.

  3. Hi, I want to convert any type for file into flv. plz give me suggestion what will i have to use code in php

  4. Hi Md Nezamuddin! You may be surprised to find out that ffmpeg will work with a LOT of file formats. Using the code on this blog post you may actually be able to convert your video. It has worked for me with mpg, mov, …. Give it a try!

  5. i have my configuration settings all right…………..
    i am able to upload file size more than 1mb …………..
    but in case of flv convertion it failed…………..

  6. I used -qmin and -qmax but quality of video goes down drastically as I want the reduced file size… n m getting reduced or required file size with -qmin 20 and -qmax 31

    plz. give me some solution.

    thanx

  7. Hello Sir,

    can u suggest me with a java code which will run the above ffmpeg command. i have done something like

    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec(“cmd.exe /”ffmpeg/” -i /”230210035600.wmv/” -deinterlace -ar 44100 -r 25 -qmin 3 -qmax 6 /”230210035600.flv/””);

    But the code is not working. it is not converting the file.

    please suggest

  8. Unfortunately, I cannot help you since Java is not my forte. However, it’s entirely possible other readers will be able to chime in with some advice. Thanks for reading my blog and I hope your problem will be resolved soon :)

  9. Very good, my friend.
    You helped me a lot.

    I had to make a small improve to the script, since your ffmpeg line didn’t save any metadata into the new flv file. After some research, I gave trying to add directly to that line and started to look for external tools to the the job.

    The most famous one is FLVTOOL2, but since it’s coded in ruby, and I don’t even know how to install, I decided to move on and continue searching.

    Then I found YAMDI (http://yamdi.sourceforge.net/). I found it easier to install and run. Besides that, it’s much faster than FLVTOOL2.

    So I took 2 steps:

    1) Install YAMDI
    See the simple how to on it’s website.

    2) Changed the script to automatically add the metadata stuff

    You may notice that I removed the strtolower and the space replacing functions, that’s because I’m using the filename as it’s own description on a website that I’m developing.

    That’s it.
    Thank’s again.

    []’s

  10. Well, the code was not presented on my comment.
    Let me try again

    <?php
    if ($handle = opendir(‘.’)) {
    while (false !== ($file = readdir($handle))) {

    if ($file != “.” && $file != “..” && strpos($file, “mpg”) !== false) {
    $new_filename = str_replace(“mpg”, “flv”, $file);
    $file = str_replace(” “, “\ “, $file);
    $new_filename = str_replace(” “, “\ “, $new_filename);
    exec(“ffmpeg -i $file -deinterlace -ar 44100 -r 25 -qmin 2 -qmax 4 temp.flv”);
    exec(“yamdi -i temp.flv -o $new_filename”);
    exec(“rm temp.flv”);
    }
    }
    closedir($handle);
    }
    ?>

  11. Duh!

    I forgot to tell why I had to add those stuff to the script.
    Without the metadata, FLV players, such as flowplayer, don’t show de total time and controls for the video.

    Now I’m gone! =]

  12. hello thank you for your script!

    i have finally gotten ffmpeg installed on my server
    but when i try to execute i get an error:

    -bash: syntax error near unexpected token `newline’

    do you know why maybe this happens?

    thanks!

  13. Hi,

    Can any1 please tell me, from where should i download ffmpeg and how should i put that in the server?

  14. Thanks! Small and very clear post.

    your qmin and qmax explanation helps me alot. Thank you so much

Comments are closed.