FFmpeg Tutorial: Convert MTS/M2TS(AVCHD) to AVI

This tutorial will show how to install FFmpeg and the required libraries, that give FFmpeg support for the codecs required to convert AVCHD videos.
It is faster to install FFmpeg than m2tstoavi and works with Sony HDR-SR7 camera. Another subscriber sent me an email and told me he got it working with his Sony HDR-SR12E camcorder using this method. Please comment if you were able to covert videos from your camera with these instructions.
The following cameras have been tested and work with these steps.
- Panasonic HDC-SD 100
- Panasonic HDC-SD9
- Sony HDR-SR11
- Sony HDR-CX12
- Canon Legria HF200E HD
Install the Dependencies
1. Use apt-get to retrieve all of the packages needed to install FFmpeg and x264 (make sure you enable the universe and multiverse repositories):
sudo apt-get install build-essential libxvidcore4-dev libfaad-dev libfaac-dev libmp3lame-dev subversionInstall FFmpeg
2. Get the most current source files from the official FFmpeg svn.
svn checkout svn://svn.ffmpeg.org/ffmpeg/trunk ffmpeg3. Now compile FFmpeg with the ./configure command. Depending on which features you want to enable, the switches add support for different video and audio codecs.
cd ffmpeg
./configure --enable-gpl --enable-libmp3lame --enable-libxvid --enable-libfaac --enable-nonfree
make
sudo make installUsing FFmpeg
4. Find out what video/audio bitrate, resolution,PAR(pixel aspect ratio),DAR(display aspect ratio) with the ffmpeg -i input.mts command.
$ ffmpeg -i input.mts
Input #0, mpegts, from 'input.mts':
Duration: 00:00:37.56, start: 1.000067, bitrate: 9440 kb/s
Program 1
Stream #0.0[0x1011]: Video: h264, yuv420p, 1440x1080 [PAR 4:3 DAR 16:9], 59.94 tbr, 90k tbn, 59.94 tbc
Stream #0.1[0x1100]: Audio: ac3, 48000 Hz, 5.1, s16, 448 kb/s- video bitrate = 9440 kb/s
- pixel aspect ratio[PAR]: 4:3
- display aspect ration[DAR]: 16:9
- audio bitrate = 448kb/s
5. Now fill in the ffmpeg switches, depending on the information gathered from the previous step.
ffmpeg -i input.mts -vcodec libxvid -b 18000k -acodec libmp3lame -ac 2
-ab 320k -deinterlace -s 1440x1080 output.avi*Sony HDR-SR7 1080i HD (1440x1080) uses interlaced frames, so use the deinterlace switch.
Success, AVCHD converted to AVI
6. If all went well, run the output.avi through ffmpeg -i, to check how well the encoding went
$ ffmpeg -i ouput.avi
Input #0, avi, from 'output.avi':
Duration: 00:00:19.08, start: 0.000000, bitrate: 9564 kb/s
Stream #0.0: Video: mpeg4, yuv420p, 1440x1080 [PAR 4:3 DAR 16:9], 59.94 tbr, 59.94 tbn, 59.94 tbc
Stream #0.1: Audio: mp3, 48000 Hz, stereo, s16, 320 kb/sNeed Help?
If you need help feel free to contact and ask your questions using my contact form. I'll try to give you a good answer.

Comments
Works awesome, thanks heaps for this!
If I may just add one thing, I also needed to add 'libxvidcore4-dev' to the initial apt-get clause
Hey Podecoet
Thanks for the feedback, xvid is diffidently needed.
Hi i want to now, how i can make my avi files with 5.1 sound? I try with "ffmpeg -i 00080.MTS -vcodec libxvid -b 18000k -acodec ac3 -ac 5 -ab 448k -deinterlace -s 1440x1080 output.avi" but mplayer and totem only show the video without sound thanks for the manual.
Hi kortux,
I think the problem is the -ac switch, try using 6 instead of 5.
ffmpeg -i 00080.MTS -vcodec libxvid -b 18000k -acodec ac3 -ac 6 -ab 448k -deinterlace -s 1440x1080 output.avi"I believe the fact is you don't have to put -ac = 5 or 6 cause ac3 already means 5.1 , i may be wrong but i believe you have to do it without specifying the -ac parameter it would be like this ffmpeg -i 00080.MTS -vcodec libxvid -b 18000k -acodec ac3 -deinterlace -s 1440x1080 output.avi"
Beautiful...worked like a charm....I wonder how difficult it is to come up with a GUI based on this command....thanks a lot my friend..bless you
Thanks, works for me with Panasonic HDC-SD 100 on Kubuntu jaunty on a five year old desktop.
Thanks!
Note, for my Sony HDR-CX12 I also needed to set the audio sampling rate via the "ar" switch:
ffmpeg -i input.mts -vcodec libxvid -b 18000k -acodec libmp3lame -ac 2
-ab 320k -ar 44100 -deinterlace -s 1440x1080 output.avi
Otherwise, I was receiving this error message: "Could not write header for output file #0 (incorrect codec parameters ?)"
cheers.
I got it working with your instructions on my Sony HDR-SR11 10MP 60GB High Definition Hard Drive Handycam Camcorder. I also added the deinterlace switch. Thanks so much!!
Super!
Works like a charm with .MTS files from my Canon Legria HF200E HD cam (also using the -deinterlace switch).
Thank you
Thank you for a very informative and helpful post.
I do have one question regarding the bitrate switch. According to the ffmpeg documentations the bitrate is in bits/second. You set this to 18000k yet output.avi is shown to have a bitrate of 9564 kb/s. Why is this?
Best regards,
Þórir Már
Thank you for this great and straightforward tutorial. Footage recorder with my Sony HDR-SR11 now can be converter to a Xvid and shared with my family. Thank you so much!
Jarek.
works great using Panasonic HDC-SD9
hi
thanks for the tutorial. I'm using a sony cx100 an thats the output with ffmpeg -i:
Duration: 00:48:35.22, start: 599.960067, bitrate: 7790 kb/s
Program 1
Stream #0.0[0x1011]: Video: h264, yuv420p, 1440x1080 [PAR 4:3 DAR 16:9], 50 tbr, 90k tbn, 50 tbc
Stream #0.1[0x1100]: Audio: ac3, 48000 Hz, stereo, s16, 256 kb/s
any ideas on how to set the switches? the audio from the converted file is not in sync with the video, by about a few seconds.
would be great if you could help me, I have been on this for quite a while now.
Look at your "tbr" and "tbc" data pieces. They are 50, not 59.8. This means your camcorder is PAL (25fps), not NTSC (29.97fps). You'll want to change the fps in the ffmpeg command to match - that'll fix your audio syncing issues.
Hello
I am having trouble getting ffmpeg to find my file, So I can carry out step 4?
If have it stored on the desktop and it is called 'Grand'.
do I type- ffmpeg -i sasha\desktop\grand.m2ts
or do I type- ffmpeg -i "sasha\desktop\grand.m2ts"
Assistance in this would be much appretiated
Sasha
Linux is case-sensitive, so "desktop" and "Desktop" are two separate folders.
Try
ffmpeg -i /home/sasha/Desktop/grand.m2tsYou could also use "~" alias. i.e
ffmpeg -i ~/Desktop/grand.m2tsBoth example point to the same place. The first example is the full path, and the second example is a relative path.
This seems to work for a Cannon HF-10 shooting at maximum quality.
ffmpeg -i input.m2ts -vcodec libxvid -b 16717k -acodec libmp3lame -ac 2 -ab 256k -deinterlace -s 1920x1080 output.avi
Thanks for the tutorial
Paul
Great!
Work very nice with my JVC Everio hm200!
Nautilus scripts!!!!
I will help who uses gnome.
install the nautilus-scripts
after that go to : ~/.gnome2/nautilus-scripts
and create a file called MTStoAVI, and put the following content in it:
#!/bin/bash
for a in `ls *.MTS` ; do ffmpeg -i $a -vcodec libxvid -b 18000k -acodec libmp3lame -ac 2 -ab 320k -deinterlace -s 1280x720 `echo "$a" | cut -d'.' -f1`.avi ; done
exit
after that type:
chmod +x ~/.gnome2/nautilus-scripts/MTStoAVI
And that is all.
now open nautilus and enter in the directory with MTS files, right-click anywhere and select: Scripts -> MTStoAVI
Done!
all yours MTS files will be converted to avi XD
Regards
If anyone can help me with what my ffmpeg command should look like it would be much appreciated. I can get video but no sound. Here is my output from ffmpeg -i:
[mpegts @ 0x1c1d3c0]max_analyze_duration reached
Input #0, mpegts, from '00097.MTS':
Duration: 00:00:29.05, start: 0.457244, bitrate: 5898 kb/s
Program 1
Stream #0.0[0x1011]: Video: h264, yuv420p, 1440x1080 [PAR 4:3 DAR 16:9], 59.92 fps, 59.94 tbr, 90k tbn, 59.94 tbc
Stream #0.1[0x1100]: Audio: ac3, 48000 Hz, stereo, s16, 256 kb/s
And here is what I thought that should mean:
ffmpeg -i 00097.MTS -vcodec libxvid -b 5898k -acodec libmp3lame -deinterlace -ac 2 -ab 256k -ar 48000 -s 1440x1080 00097.avi
I get a nice looking video, but there is no sound.
Thanks!
Iain
I am using WinFF in converting my MTS files to NTSC-DVD quality mpeg files. However, I encounter problem with ffmpeg I installed through synaptic. This guide solved the problem for me. For a quick look of WinFF generate code I wrapped in bash script function
function convert_using_ffmpeg {ffmpeg -i $1 \
-threads 2 \
-f dvd -target ntsc-dvd -r 29.97 -s 720x480 -aspect 16:9 \
-b 8000kb -g 12 -mbd rd -trellis -mv0 -cmp 0 -subcmp 2 \
$2
}
I got two questions:
1) As Þórir Már indicated, why set 18000k while mts file shows a bitrate of 9564kb/s?
2) After converting mts to avi, ffmpeg -i file.avi shows negative bitrate as below, anybody can help me? Thank you.
----------mts info---
ffmpeg -i 00001.mts
Input #0, mpegts, from '00001.mts':
Duration: 00:01:00.06, start: 0.469922, bitrate: 23974 kb/s
Program 1
Stream #0.0[0x1011]: Video: h264, yuv420p, 1920x1080 [PAR 1:1 DAR 16:9], 59.94 tbr, 90k tbn, 59.94 tbc
Stream #0.1[0x1100]: Audio: ac3, 48000 Hz, stereo, s16, 256 kb/s
At least one output file must be specified
-----------convert-------
ffmpeg -i 00001.mts -vcodec libxvid -b 24000k -acodec libmp3lame -ac 2 -ab 320k -deinterlace -s 1920x1080 00001.avi
Press [q] to stop encoding
frame= 207 fps= 8 q=6.0 size= 11674kB time=6.96 bitrate=13741.0kbits/s <<<<------ why only 13741kb/s?
----------avi info-------
ffmpeg -i 00001.avi
Input #0, avi, from '00001.avi':
Duration: 00:00:00.00, start: 0.000000, bitrate: -2147483 kb/s <<<<----???
Stream #0.0: Video: mpeg4, yuv420p, 1920x1080 [PAR 1:1 DAR 16:9], 59.94 tbr, 59.94 tbn, 59.94 tbc
Stream #0.1: Audio: mp3, 48000 Hz, stereo, s16, 320 kb/s
At least one output file must be specified
Thank you.
Hi all !
Just wanted to let you know that kdenlive in debian lenny can natively work with the AVCHD files from my Lumix DMC-TZ7.
Cheers,
Karl
Hi all. Happy new year!
I have some problems with my camcorder Panasonic HDC-SD10
1)
ffmpeg -i 00000.mts
FFmpeg version SVN-r13582, Copyright (c) 2000-2008 Fabrice Bellard, et al.
configuration: --prefix=/usr --libdir=${prefix}/lib --shlibdir=${prefix}/lib --bindir=${prefix}/bin --incdir=${prefix}/include/ffmpeg --enable-shared --enable-libmp3lame --enable-gpl --enable-libfaad --mandir=${prefix}/share/man --enable-libvorbis --enable-pthreads --enable-libfaac --enable-libxvid --enable-postproc --enable-libamr-nb --enable-libamr-wb --enable-x11grab --enable-libgsm --enable-libx264 --enable-liba52 --enable-libtheora --extra-cflags=-Wall -g -fPIC -DPIC --cc=ccache cc --enable-swscale --enable-libdc1394 --enable-nonfree --disable-mmx --disable-stripping --enable-avfilter --enable-libdirac --disable-decoder=libdirac --enable-libschroedinger --disable-encoder=libschroedinger --disable-altivec --disable-armv5te --disable-armv6 --disable-vis
libavutil version: 49.7.0
libavcodec version: 51.58.0
libavformat version: 52.16.0
libavdevice version: 52.0.0
libavfilter version: 0.0.0
built on May 3 2009 12:07:18, gcc: 4.3.2
Input #0, mpegts, from '00000.mts':
Duration: 00:06:14.88, start: 0.841589, bitrate: 13350 kb/s
Program 1
Stream #0.0[0x1011]: Video: h264, yuv420p, 1920x1080 [PAR 1:1 DAR 16:9], 25.00 tb(r)
Stream #0.1[0x1100]: Audio: ac3, 48000 Hz, stereo, 256 kb/s
Must supply at least one output file
Question: What does 25.00 tr(r) means?
Convert:
ffmpeg -i 00000.mts -vcodec libxvid -b 13350k -acodec libmp3lame -ac 2 -ab 256k -ar 48000 -s 1920x1080 -deinterlace output.avi
Input #0, mpegts, from '00000.mts':
Duration: 00:06:14.88, start: 0.841589, bitrate: 13350 kb/s
Program 1
Stream #0.0[0x1011]: Video: h264, yuv420p, 1920x1080 [PAR 1:1 DAR 16:9], 25.00 tb(r)
Stream #0.1[0x1100]: Audio: ac3, 48000 Hz, stereo, 256 kb/s
Output #0, avi, to 'output.avi':
Stream #0.0: Video: libxvid, yuv420p, 1920x1080 [PAR 1:1 DAR 16:9], q=2-31, 13350 kb/s, 25.00 tb(c)
Stream #0.1: Audio: libmp3lame, 48000 Hz, stereo, 256 kb/s
Stream mapping:
Stream #0.0 -> #0.0
Stream #0.1 -> #0.1
Press [q] to stop encoding
[h264 @ 0x7f729d2006c0]B picture before any references, skipping
[h264 @ 0x7f729d2006c0]decode_slice_header error
[h264 @ 0x7f729d2006c0]no frame!
Error while decoding stream #0.0
[h264 @ 0x7f729d2006c0]B picture before any references, skipping
[h264 @ 0x7f729d2006c0]decode_slice_header error
[h264 @ 0x7f729d2006c0]no frame!
Error while decoding stream #0.0
[h264 @ 0x7f729d2006c0]B picture before any references, skipping
[h264 @ 0x7f729d2006c0]decode_slice_header error
[h264 @ 0x7f729d2006c0]no frame!
Error while decoding stream #0.0
[h264 @ 0x7f729d2006c0]B picture before any references, skipping
[h264 @ 0x7f729d2006c0]decode_slice_header error
[h264 @ 0x7f729d2006c0]no frame!
Error while decoding stream #0.0
[h264 @ 0x7f729d2006c0]PAFF + spatial direct mode is not implemented
[h264 @ 0x7f729d2006c0]PAFF + spatial direct mode is not implemented
[h264 @ 0x7f729d2006c0]PAFF + spatial direct mode is not implementedits/s
[h264 @ 0x7f729d2006c0]PAFF + spatial direct mode is not implemented
[h264 @ 0x7f729d2006c0]PAFF + spatial direct mode is not implemented
[h264 @ 0x7f729d2006c0]PAFF + spatial direct mode is not implemented
Last message repeats many times - what does it mean?
Here is the output.avi file
ffmpeg -i output.avi
FFmpeg version SVN-r13582, Copyright (c) 2000-2008 Fabrice Bellard, et al.
configuration: --prefix=/usr --libdir=${prefix}/lib --shlibdir=${prefix}/lib --bindir=${prefix}/bin --incdir=${prefix}/include/ffmpeg --enable-shared --enable-libmp3lame --enable-gpl --enable-libfaad --mandir=${prefix}/share/man --enable-libvorbis --enable-pthreads --enable-libfaac --enable-libxvid --enable-postproc --enable-libamr-nb --enable-libamr-wb --enable-x11grab --enable-libgsm --enable-libx264 --enable-liba52 --enable-libtheora --extra-cflags=-Wall -g -fPIC -DPIC --cc=ccache cc --enable-swscale --enable-libdc1394 --enable-nonfree --disable-mmx --disable-stripping --enable-avfilter --enable-libdirac --disable-decoder=libdirac --enable-libschroedinger --disable-encoder=libschroedinger --disable-altivec --disable-armv5te --disable-armv6 --disable-vis
libavutil version: 49.7.0
libavcodec version: 51.58.0
libavformat version: 52.16.0
libavdevice version: 52.0.0
libavfilter version: 0.0.0
built on May 3 2009 12:07:18, gcc: 4.3.2
Input #0, avi, from 'output.avi':
Duration: 00:12:29.60, start: 0.000000, bitrate: 12126 kb/s
Stream #0.0: Video: mpeg4, yuv420p, 1920x1080 [PAR 1:1 DAR 16:9], 25.00 tb(r)
Stream #0.1: Audio: mp3, 48000 Hz, stereo, 256 kb/s
Must supply at least one output file
Input MTS file is 6min long, output is 12min?
Thanks for your tutorial. It works with a Canon Vixia HG-20. This .MTS file:
Input #0, mpegts, from '00067.MTS':
Duration: 00:09:58.11, start: 0.484922, bitrate: 16345 kb/s
Program 1
Stream #0.0[0x1011]: Video: h264, yuv420p, 1920x1080 [PAR 1:1 DAR 16:9], 59.92 fps, 59.94 tbr, 90k tbn, 59.94 tbc
Stream #0.1[0x1100]: Audio: ac3, 48000 Hz, stereo, s16, 256 kb/s
Was converted easily with the same command (with the -s flag adjusted for resolution) as you suggested. Thanks again!
This seems to have worked well under windows as well... I have a Panasonic hdc-sd9.
This works fine with my Panasonic/Lumix GH1 and Fedora 12.
Post new comment