Home > linux, unix > DeMuxing and ReMuxing M2TS/MP4/FLV to MKV under –insert-your-preferred-unixoid-os-here–

DeMuxing and ReMuxing M2TS/MP4/FLV to MKV under –insert-your-preferred-unixoid-os-here–

Sometimes I get some video files in mp4, flv or even m2ts containers. Since I am a big, big fan of the mkv container format, I usually remux those files. When I first tried to find a solution for this using Linux, Google spit out everything and the kitchen sink, but not the most obvious choices: use ffmpeg or mplayer.

To demux with ffmpeg:

ffmpeg -i input.{mp4,flv,m2ts} -vcodec copy out.{h264,m2v} -acodec copy out.{aac,mp3}

AFAIK mplayer involves two steps to demux video and audio separately:

mplayer -dumpvideo -dumpfile out.{h264,m2v} input.{mp4,flv,m2ts}
mplayer -dumpaudio -dumpfile out.{aac,mp3} input.{mp4,flv,m2ts}

There are probably a gazillion ways more (mencoder comes to mind) to achieve the demuxed material and if the input files contain more than one video and one audio stream, you might have to – personally I never was in such a scenario – dig up ffmpeg’s manual about stream selection. Which actually is pretty easy to do.

Once you got your streams demuxed, use the most excellent mkvtoolnix suite to remux them into a mkv container (note: mmg is your friend here). As you will be most likely adding elementary streams, mkvmerge won’t be able to automatically determine the correct fps. Just manually enter them, ffmpeg or mplayer should have spit them out in the demuxing process. Here you can also add subtitles and w/e your heart desires into the container.

out.{h264,m2v} input.{mp4,flv,m2ts}
Advertisements
Categories: linux, unix Tags: , , ,
  1. crazynerv
    June 12, 2010 at 7:58 am

    helpful ta

  2. Thomas Weyn
    September 10, 2010 at 5:16 pm

    Thank you! This was very helpful!

    • Gordon Schulz
      September 10, 2010 at 5:58 pm

      Glad it helped. 🙂

  3. Rohit
    December 31, 2011 at 1:02 am

    Thanks, it worked great!

  4. Querk McGoober
    January 10, 2012 at 6:45 pm

    Looked for ages to find a mux/demuxer to adapt some vids I was worky on. Then found I could use ffmpeg, mencoder and such like.

    Now the billions of dollars are in my bank not these rip off merchants who use these free tools to profit…
    They need shooting very quickly

  5. April 8, 2012 at 9:58 pm

    Thank you for your post. I have used and it has been proofed to be very useful

  6. April 9, 2012 at 12:39 pm

    googling I found this
    ffmpeg -i filename.flv -vcodec copy -acodec copy filename.mkv

    This has worked when the code above failed

    • JFG
      February 10, 2013 at 7:52 pm

      Thanks for sharing, this fixed it for me! I was having problem with somehow corrupted FLVs that wouldn’t properly demux but remuxing them to MKVs in one operation somehow worked flawlessly.

      Thanks again!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: