2013-03-10

HOWTO: losslessly convert FLV video to MP4 in Linux (Debian)

Well, let's imagine we have a Flash Video (FLV/F4V) container file (possibly "unseekable" or with other minor problems) containing MPEG-4 AVC video & AAC (or MP3) audio.

We'll convert ("remux") it to MP4 without any transcoding (re-encoding), so it'll be very fast, and using only the free tools (available in the official Debian GNU/Linux repositories).

Note: unfortunately, old FLV videos containing VP6 and Sorenson video formats can't be remuxed into MP4 format; if you don't want re-encoding, you can try to remux them into MKV (Matroska); if you just want to fix the seek problems, you can just try FLV-to-FLV remuxing.

  • Method #1 (recommended): use ffmpeg (it's the main utility from the FFmpeg project).
    Install the "ffmpeg" package:
    # apt-get install ffmpeg
    Convert a FLV file to MP4, preserving audio & video:
    $ ffmpeg -i input.flv -codec copy output.mp4
    And even optimizing it for faster loading:
    $ ffmpeg -i input.flv -codec copy -movflags faststart output_optim.mp4
    To extract audio only:
    $ ffmpeg -i input.flv -codec copy -vn output_a.mp4
    To extract video only:
    $ ffmpeg -i input.flv -codec copy -an output_v.mp4
    Note: you can also use M4A (audio) / M4V (video) file extensions for output, if you like those Apple formats.
  • Method #2 (alternative): using avconv (it's a part of Libav project, a fork of FFmpeg).
    Install the "libav-tools" package:
    # apt-get install libav-tools
    Convert a FLV file to MP4, preserving audio & video:
    $ avconv -i input.flv -codec copy output.mp4
    To extract audio only:
    $ avconv -i input.flv -codec copy -vn output_a.mp4
    To extract video only:
    $ avconv -i input.flv -codec copy -an output_v.mp4
  • Method #3 (a somewhat ugly one): using MEncoder (a part of the MPlayer project).
    Install MEncoder:
    # apt-get install mencoder
    Convert a FLV file to MP4:
    $ mencoder input.flv -ovc copy -oac copy -of lavf -o output.mp4
    Note: according to the warning from MEncoder itself, output file can be corrupt:
    ** MUXER_LAVF *****************************************************************
    REMEMBER: MEncoder's libavformat muxing is presently broken and can generate
    INCORRECT files in the presence of B-frames. Moreover, due to bugs MPlayer
    will play these INCORRECT files as if nothing were wrong!
    *******************************************************************************
    So probably it suggests that you should better try the MEncoder's favorite AVI format:
    $ mencoder input.flv -ovc copy -oac copy -o output.avi
    (But that's not what we want here, right?)

Source software packages availability for Debian releases:
  • ffmpeg: up to Debian 6 "Squeeze", and then again from 9 "Stretch" (also in 8 "Jessie" backports);
  • libav: starting from 7 "Wheezy" (also in 6 "Squeeze" backports), up to 8 "Jessie";
  • mplayer: up to 7 "Wheezy", and then from 9 "Stretch".

And yes, it should work for Ubuntu too; any current FFmpeg version should also work in other GNU/Linux or *BSD distros, MS Windows, Apple Mac OS X etc.

References:
  1. ffmpeg Documentation
  2. Libav documentation : avconv
  3. 6.1. Selecting codecs and container formats - Chapter 6. Basic usage of MEncoder (MPlayer - The Movie Player documentation)
  4. Wikipedia:
    1. Flash Video
    2. MPEG-4 Part 14
    3. M4V
    4. H.264/MPEG-4 AVC
    5. Advanced Audio Coding
    6. Audio Video Interleave
---
Last updated: 2017-12-28

5 comments:

  1. I use Avdshare Video Converter to convert FLV to MP4,MOV, MKV, AVI, WMV, MPG, etc
    Here is the easy guide https://www.avdshare.com/flv-to-mp4-downloader-converter

    ReplyDelete
    Replies
    1. I'm afraid you missed the point.
      Your program is a clumsy closed-source Win/Mac-only shareware that can't even do a lossless remux.
      And more to say, it uses FFmpeg internally!

      Delete
  2. I use iDealshare VideoGo to batch convert FLV to MP4, AVI, WMV, MOV, VOB, DV, MKV, MPEG, AVCHD, 3GP, TS, RMVB, etc.
    Here is the easy guide https://www.idealshare.net/video-converter/convert-flv-to-mp4-on-mac-windows.html

    ReplyDelete
    Replies
    1. Oh no, it's another useless shareware Win/Mac application using FFmpeg internally! ;)

      Delete
  3. Quite a nice post, and I've found another detailed guide on how to convert MKV to MP4 using FFmpeg here, besides, alternative to FFmpeg is mentioned in this guide.

    ReplyDelete