Convert MTS/M2TS(AVCHD) to AVI

In this tutorial I will describe how to configure and install m2tstoavi from command line, on Ubuntu 7.10 (Gutsy Gibbon).

The m2tstoavi script was written by Axel Olmos, and is the easiest way to convert mts/m2ts video files into an avi. Here is the work flow I followed to get m2tstoavi installed.

By default Ubuntu 7.10 does not have either universe or multiverse enabled. You can skip this step if you already have universe and multiverse enabled.

1. Enable universe and multiverse repositories.

From command line, open /etc/apt/sources.list

sudo gedit /etc/apt/sources.list

Uncomment these lines, and substitute gutsy to match your Ubuntu version (i.e hardy,feisty) :

deb http://us.archive.ubuntu.com/ubuntu gutsy universe
deb http://us.archive.ubuntu.com/ubuntu gutsy multiverse

Finally update your packages list.

sudo apt-get update

2. Install the packages required by the m2tstoavi script.

Grab all the dependent packages, with this command:

sudo apt-get install mplayer ffmpeg a52dec mencoder x264 faad faac libfaad2-0 libfaad2-dev subversion csh build-essential

3. Download

Obtain the m2tstoavi script from here. Or use wget to download it from command-line.

wget http://www.olmosconsulting.com/m2ts.tar.gz

Untar the file, and cd into the directory

tar zxvf m2ts.tar.gz
cd m2ts

4. Configure

The download script needs to be configured with the current version of H.264/AVC reference software.

sudo gedit download

Modify the following line, to current version of #H.264/AVC

set version=14.0

5. Install

Now start the installation script

sudo ./install

6. Using the m2tstoavi script.

Here is how to use the default conversion method. This method has a large hard drive overhead and requires approximately 4 GB / min.

m2tstoavi sample.mts

7. Recommended Method

I recommend using the alternative script, m2tstoavi.fifo. This script uses named pipes, and that reduces the amount of free space required.

First change the file’s permissions to make it executable.

sudo chmod 744 m2tstoavi.fifo

If you don’t want to type “./” before the command, move the script into your path

sudo cp m2tstoavi.fifo /usr/local/bin

Wesley thanks for the great post and helping us get through this on Ubuntu! I have a question though if you have a second? My question is what does all this converting do to the quality of the video? And does Ubuntu just recognize and mount the camera that is hooked up via usb like an hdrux1! Any help would be much appreciated thanks.

I’m not sure if the quality is affected by the conversion process. I will look into this for you.

With my HDR-SR7, I have to press the “computer” option on the [USB SELECT] screen. Then Ubuntu mounts it automatically, and puts an icon on the desktop.

This works great for me. You don’t happen to know how I could easily de-interlace the video from my Canon HG10 do you? Thanks!

Cool, I just needed to modify the ffmpeg command to add -deinterlace and -aspect 16:9

I followed all the steps you described, but when I try to use the m2tstoavi.fifo command it gives me “command not found”. What could be the problem? The installation was completed successfully I think, or at least it didn’t give me any error messages.
thanks

Another problem: I’ve just finished converting a MTS file into avi but all images are distorted and colors are changed. If I watch directly from the camera, instead, the video is perfect. Should I use a program or a particular configuration?
thanks again

If it says “command not found”, I suspect the m2tstoavi.fifo is not in your path. If you goto the directory where m2tstoavi is installed, you should be able to type ./m2tstoavi.fifo and it should work.

For your second question. Do you have a NTSC or PAL video camera(HDR-SR7 or HDR SR7E). You can locate the model on the bottom of the camera.

Hi. Sorry, but i’m a linux newbie, I’m stuck at the part where you have to ./path/install but it says that i don’t have the permission to do so. Can anyone help me?

% ./m2ts/install
./m2ts/install: Permission denied.

If it is a permissions problem then change the permissions of the folder with:
% sudo chmod 777 m2ts

Be careful with 777 because it gives everyone on that machine access to the folder.
For more information on changing file permissions check this site. Linux Permissions

Then
% sudo ./m2ts/install

Hi! This script works fine :)
Thank you
The only problem is sound :(
I have no sound after converting
My camera is SR5E

New problem!
Sound is working now (after installing some codecs)
But resulting avi is strange and can be played only by mplayer :(
when I try to play it by Kaffeine or other movie-player, time-slider starts jumping back and forward
and sound starts to work asynchronously with picture :( (speed of picture is not constant)

In response to Roberto’s comment regarding the “command not found” message with the m2tstoavi.fifo command… I had the same problem. I seem to have fixed it my changing the permission on it to 744, and opening it with “./m2tstoavi.fifo ________.mts”. In other words, for me it seems that the command required a “./” prior to the text whereas the normal command did not. Hope this helps…

To all with the “command not found” problem: Just copy the m2tstoavi.fifo script into your /usr/local/bin folder, just like the other scripts:

sudo chmod 744 m2tstoavi.fifo
sudo cp m2tstoavi.fifo /usr/local/bin

The script is then in your path and you can omit the ./ before the command.

Thank you Peter for your insight into the “command not found” problem”. I have added the steps you have mentioned to the how to.

After installing I view the samplevideo.avi file and it is 2 seconds of black. Are there some codec’s I am missing?

Chris, after you used the install script, did the samplevideo automatically play on your screen?

What program did you use to view the video?

My setup is a headless linux box running Ubuntu 7.10 server. I have to pull the samplevideo off to view. I’ve been able to get the video to convert, but there is no audio when I copy the resulting AVI to a Windows XP box. Thanks for your help.

@Chris

My suggestion would be to try and view through VLC. By default the video is converted into mpeg4, and the audio is AC3, inside an AVI container.

Please let me know if this helps.

Greetings. Thanks for putting this together. I ran through the instructions and got the sample video converted at the end of the install but am having problems running the script using my files. When I try to execute the script I get:

/usr/local/bin/m2tstoavi.fifo: line 1: #!/bin/csh: No such file or directory
/usr/local/bin/m2tstoavi.fifo: line 11: syntax error near unexpected token `(’
/usr/local/bin/m2tstoavi.fifo: line 11: ` set files=($*)’

here’s the file itself:
#!/bin/csh

#The scripts and instructions in this package are free to use and
#redistribute AT YOUR OWN RISK!! Standard disclaimers apply.
#NO WARRANTY!

if ( $#argv == “0″ ) then
echo usage: $0 filename.m2ts …
exit
else
set files=($*)
endif

I do have a csh file in the bin folder (a link to executable actually), but I can’t create a folder due to that link being there.

If anyone has any suggestions I’d be much obliged.

Thanks!

Ok, finally got the script to run again by deleting the first line of script and retyping it but now I get the following problem:

Cannot open Annex B ByteStream file ‘bits0001.mpv’

Any suggestions?

Thanks!

@retlaw

I think I found the problem, please confirm. The problem was step 5 needs to be ran as root, with sudo.

Hi, when I try to run “m2tstoavi.fifo 00000.mts” in my video folder it says: “using: xporthdmv: Command not found.”
Both m2toavi and -fifo do the same. I’m using ubuntu 8.04 and commands should be installed properly. Am I missing this “xporthdmv” -part? Thx a lot!

@Lauri

I suspect that you are missing xporthdmv.

To verify if xportdmv is installed, run this from command line

which xporthdmv

If xporthdmv is installed it will output something like this:

/usr/local/bin/xporthdmv

Hello. I’m having a tough time getting this to work properly. I’m using Ubuntu 64 bit with the following error:

petah@desky:/media/SataTwo/Camcorder/BDMV/STREAM$ m2tstoavi.fifo *
using:
/usr/local/bin/xporthdmv
/usr/local/bin/ldecod
/usr/bin/ffmpeg
/usr/local/bin/m2tstoavi.fifo Starting.

xporthdmv -hn 00000.MTS 1 1 1
Unsupported Option: n
Cannot open video output file
mkfifo: cannot create fifo `/tmp/00000.yuv’: File exists
ldecod -i bits0001.mpv -o /tmp/00000.yuv
[1] 7107
ffmpeg -r 29.97 -s 1440×1080 -vcodec mpeg4 -sameq -i /tmp/00000.yuv -acodec copy -i /tmp/00000.ac3 -b 15000k 00000.avi
FFmpeg version SVN-rUNKNOWN, Copyright (c) 2000-2007 Fabrice Bellard, et al.
configuration: –enable-gpl –enable-pp –enable-swscaler –enable-pthreads –enable-libvorbis –enable-libtheora –enable-libogg –enable-libgsm –enable-dc1394 –disable-debug –enable-shared –prefix=/usr
libavutil version: 1d.49.3.0
libavcodec version: 1d.51.38.0
libavformat version: 1d.51.10.0
built on Mar 12 2008 15:36:03, gcc: 4.2.3 (Ubuntu 4.2.3-2ubuntu4)
—————————– JM 14.0 (FRExt) —————————–
Decoder config file : (null)
————————————————————————–
Input H.264 bitstream : bits0001.mpv
Output decoded YUV : /tmp/00000.yuv
Output status file : log.dec
Input reference file : test_rec.yuv does not exist
SNR values are not available
————————————————————————–
POC must = frame# or field# for SNRs to be correct
————————————————————————–
Frame POC Pic# QP SnrY SnrU SnrV Y:U:V Time(ms)
————————————————————————–
Cannot open Annex B ByteStream file ‘bits0001.mpv’

If anyone has any suggestions I’d be extremely grateful

@Petah

Thanks for posting all the required details.

Try running Step 5 as root, that should fix the problem

Wes,

Thank you very much! I got it to run.

I reinstalled and followed the tutorial exactly and as long as I execute the m2tstoavi.fifo from the m2ts folder that I unzipped it works great. If I change into the folder the files are in I still get the above error, which is still a huge help. Now I just have to figure out how to change the output folder since the videos are on a separate hard drive, and the drive with the m2ts folder (aka the location that runs the file properly is on aa smaller drive. I guess I can whole swap the folder to my larger drive.

Thanks again for the help. You’ve let me move to my new build computer free from the monolithic beast of vista!

Wes,

Disregard my problem running it in the media location folder. Looks like my second disk is a read only environment. Time for some google work.

Thanks again!

Petah

I am glad to here that you were able to free yourself from the monolithic beast of vista.

Let me know if you need a hand getting your second drive mounted with read/write.

Wes,

Appreciate it. All good to go. Had a dual install with OS X so I had to disable Journaling which in turn allowed me more than read permission on the disk. I’m currently chewing through the conversions. Now I just have to pick which editing software I’m going to use. What’s your recommendation?

Thanks!

Wes,

I, too, get the “xporthdmv: Command not found.” error when I try to invoke m2tstoavi script. I’m running Hardy with the latest patches.

I did notice that the compiler script threw the following errors when I ran it. I executed it as su:

Run the compile script to compile the software.
The ./configure script has ended.

Starting ./compile script.
xport.c:5:19: error: stdio.h: No such file or directory
xport.c:6:20: error: stdlib.h: No such file or directory
xport.c:7:20: error: string.h: No such file or directory
xport.c:29: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
xport.c: In function 'main':
...

cp: cannot stat `bin/ldecod.exe’: No such file or directory
chmod: cannot access `../xporthdmv’: No such file or directory
chmod: cannot access `../ldecod’: No such file or directory

Thanks!
Fraize

@Fraize

It seems that your Ubuntu does not have the ability to compile programs. In order to compile anything in Ubuntu, you need a package called build-essential.

sudo apt-get install build-essential

Hi Wes,

thanks for your tutorial. Unfortunately I’m not able to install it.
Already step 2 fails on my machine (UbuntuStudio 8.04).

> ’sudo apt-get install mplayer ffmpeg a52dec mencoder x264 faad faac libfaad2-0 libfaad2-dev subversion csh’

leads to the first error:

Paket a52dec ist ein virtuelles Paket, das bereitgestellt wird von:
liba52-0.7.4-dev 0.7.4-11ubuntu1
Sie sollten eines explizit zum Installieren auswählen.
E: Paket a52dec hat keinen Installationskandidaten

Description is unfortunately in german. It says said a52dec does not posses a installation candidate !?

If I replaced ‘a52dec’ by ‘liba52-0.7.4-dev’ it leads to the next error:

‘Die folgenden Pakete haben nichterfüllte Abhängigkeiten:
libfaad2-dev: Hängt ab: libfaad2-0 (= 2.0.0+cvs20040908+mp4v2+bmp-0ubuntu5) aber 2.6.1-2 soll installiert werden
E: Kaputte Pakete’

It says that ‘libfaad2-dev: depends libfaad2-0 (= 2.0.0+cvs20040908+mp4v2+bmp-0ubuntu5)’,
but 2.6.1-2 is atempted to be installed –> Error: corrupted packes!?

Any ideas or suggestions?

Thanks in advance for your efforts.

Bye
Hergerger

I had trouble with the frame resolution that this script assumes. It’s assuming 1440×1080, and my camera can shoot @1920×1080. If you try to process a 1920 file with this script, you’ll get “flipping” (not sure if that’s the right term) where it looks as if you had turned the old-school “vert” knob on your TV too far one way or the other.

The fix is to edit the m2tstoavi(.fifo) script. Replace 1440 with 1920, and the video will turn out fine.

Of course, the files will only have sound in mplayer, the sound, when you do have it, is out of sync by a few seconds, and the output file won’t import into Cinelerra. Time to do some more googling…

From Convert MTS/M2TS(AVCHD) to AVI

I had to make the same change to my script. I recorded video on a Panasonic SD9 and after making the change from "1440" to "1920", the picture showed up like it was suppose to. It does look good.

I had trouble with the frame resolution that this script assumes. It’s assuming 1440×1080, and my camera can shoot @1920×1080. If you try to process a 1920 file with this script, you’ll get “flipping” (not sure if that’s the right term) where it looks as if you had turned the old-school “vert” knob on your TV too far one way or the other.

The fix is to edit the m2tstoavi(.fifo) script. Replace 1440 with 1920, and the video will turn out fine.

Of course, the files will only have sound in mplayer, the sound, when you do have it, is out of sync by a few seconds, and the output file won’t import into Cinelerra. Time to do some more googling…

From Convert MTS/M2TS(AVCHD) to AVI

hey wes. great work :)

did you find out anything about the lossyness of this procedure.

@paul

I'm sorry, I haven't heard the term "lossyness" before. I'm not sure if your referring to loss in sound or video quality.

Here is an example of the video/audio bitrates of a HD MTS video, and after the transcoded bitrates.

Original MTS File

Video bitrate: 9573 kb/s
Audio:48000 Hz, 5:1, 448 kb/s

Transcoded to AVI

Video bitrate: 461 kb/s
Audio 48000 Hz, 5:1, 448 kb/s

As you can see, there is quite a loss in the video bitrate. The audio is not affected. Hope this helps

Why is it that I can never get the ./install command to work? Here is the output:

root@ryan-laptop:/home/ryan/m2ts# ./install
bash: ./install: /bin/csh: bad interpreter: No such file or directory

Can anyone help?

The problem is the csh shell is not installed on your system.

Install csh with this command:

sudo apt-get install csh

Small correction - Step 4 should be:
sudo gedit download

Thank you for helping improve this article.

one of your first responses in this thread reads "I’m not sure if the quality is affected by the conversion process. I will look into this for you"

i should have put quotes around my 'lossyness'. converting file formats is often lossy i.e. some detail/quality is lost. i was just wondering how lossy this is. did you ever find anything out?

kbps figures dont mean much to me i'm afraid. can you *see* any difference?

I can tell you that the audio quality is unaffected(no loss). The video looks perfectly fine on my lcd monitor or standard television.

However I do believe the video quality would be noticeable on a HD televison. I will try to confirm this in the new future.

I apologize for the delay .

Thanks for the fine set of sommand scripts; At first glance, everything seems to work. Unfortunately, I encounter 2 problems:

* Playing the *avi in mplayer works fine, howeveer, in VLC I have no sound!
* When importing the *avi into cinelerra, ist pretends to be audio only, no video!

I tried several container formats and/or seperate files juggling with ffmpeg, but I can't find any container format holding both video and audio!!!

Any hints or suggestions are highly welcome!

You are correct, the default ffmpeg parameters used by m2tstoavi don't create files that can be imported into cinelerra.

These parameters have been reported to create files that can be imported into Cinelerra.

ffmpeg -i filename.mts -acodec libfaac -ab 256k -ac 2 -vcodec mpeg4 -sameq -aspect 16:9 -b 15000k -copyts output_filename.mov

audio codec = libfaac
video codec = mpeg4
container = mov

Please let me know if that helps.

Does not work for me;

:~/compilations/m2ts$ ffmpeg -i samplevideo.m2ts -acodec libfaac -ab 256k -ac 2 -vcodec mpeg4 -sameq -aspect 16:9 -b 15000k -copyts output_filename.mov
FFmpeg version SVN-rUNKNOWN, Copyright (c) 2000-2007 Fabrice Bellard, et al.
configuration: --enable-gpl --enable-pp --enable-swscaler --enable-pthreads --enable-libvorbis --enable-libtheora --enable-libogg --enable-libgsm --enable-dc1394 --disable-debug --enable-libmp3lame --enable-libfaadbin --enable-libfaad --enable-libfaac --enable-xvid --enable-x264 --enable-liba52 --enable-amr_nb --enable-amr_wb --enable-shared --prefix=/usr
libavutil version: 1d.49.3.0
libavcodec version: 1d.51.38.0
libavformat version: 1d.51.10.0
built on Apr 3 2008 18:39:35, gcc: 4.2.3 (Ubuntu 4.2.3-2ubuntu7)
[h264 @ 0x7fa34c89f8d0]PAFF interlacing is not implemented
[h264 @ 0x7fa34c89f8d0]concealing 3060 DC, 3060 AC, 3060 MV errors
Input #0, mpegts, from 'samplevideo.m2ts':
Duration: 00:00:02.4, start: 1.100100, bitrate: 16367 kb/s
Stream #0.0[0x1011]: Video: h264, yuv420p, 1440x1080, 29.97 fps(r)
Stream #0.1[0x1100]: Audio: ac3, 48000 Hz, 5:1, 448 kb/s
Unknown codec 'libfaac'

I have libfaac0 installed. When I replace libfaac with libfaac0 I get the same output.

Try substituting libfaac with libmp3lame

same result Unknown codec 'libmp3lame'

I suspect the problem is that you are using the repository verision of ffmpeg "FFmpeg version SVN-rUNKNOWN". You might need the latest svn version of svn ffmpeg.

I wrote this article, that shows you how to compile ffmpeg

Bingo ! That is it Wesley. I compiled ffmpeg with the instructions of your article and converted a samplevideo correctly. Thanks a lot for the support !

Good morning and thank you for your help converting .m2ts to.avi. It helped me.:-)
I would like to ask you a question:
Why do you use "Sudo" to untar?

With this command: sudo tar zxvf m2ts.tar.gz, is it possible to make it without using sudo. Because with sudo, a locked file is got. What I think strange.
Thank you for your answer.

Hi Patrice

Thank you for your feedback.

This tutorial documents the method I use to install m2tstoavi. When I
install m2tstoavi, I usually download it into /usr/local/bin. Which as
you know, is only writable by root.

So that is the reason, why I use "sudo" to untar, it needs root
permissions to untar in that folder.

However you raise a good point, if users download m2tstoavi to their
own personal directory. Then the sudo would not be necessary.

I will update my tutorial.

Thanks for the very helpful tutorial. I got this to work (to the point of the sample video at least) in Kubuntu 8.04.

Just in case anyone else has this - I tripped over a small problem getting it working. The x264 section of the configure script complained that I didn't have a suitable assembler, and suggested yasm. I tried installing yasm, but the script then complained that the version wasn't recent enough. It suggested nasm, which I put in - the error went away.

I also discovered that it was necessary to delete everything and start again from the extraction from the archive after messing up step 5 by failing to sudo - i.e. simply repeating the step but using sudo didn't cure the problem.

Also, do you maybe mean 755 instead of 744 in step 7?

Anyway, thanks again for the help.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
copy the characters (respecting upper/lower case) from the image.