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

This tutorial will show how to install FFmpeg, and the required libraries that you need to convert AVCHD videos. The extra libraries enable FFmpeg to decode and encode M2TS/MTS codecs into a different format, like .avi or .mkv.
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.
Listed Update Aug 12 2010
- Panasonic HDC-SD 100
- Panasonic Lumix GH1
- Panasonic GF-1
- Panasonic HDC-SD9
- Sony HDR-SR11
- Sony HDR-CX12
- Sony HDR-UX1
- Sony Handycam HDR-SR5E
- Sony HDC-HS250
- Cannon HF-10
- Canon Legria HF200E HD
- Canon Vixia HG-20
- JVC Everio hm200
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
Has anyone converted AVCHD shot on a GF1? Cineform Neoscene does not like it.
Follow the optimal settings to get excellent playback quality.
Video:
Codec: H.264
Size: 640*480 or 1280*720
Bitrate: 2500kbps
Frame rate: 30fps
Audio: 65811563
Codec: aac
Sample rate: 48khz
Bitrate: 160kbps
Channels: Stereo
Brorsoft Mac MTS/M2TS Converter is specially developed for AVCHD digital camcorder users. It aims to help videographers and amateurs to convert the high definition footages (*.mts, *.m2ts, *.m2t) to be editable in editing software like iMovie, Final Cut Express, Final Cut Pro etc with least quality loss. It also converts the AVCHD files to common video and audio formats, such as MP4, 3GP, 3G2 AVI, MPG. MOV, MKV, AVI, FLV, VOB, SWF, MP3, FLAC, OGG, WAV, AC3, M4A, MP3, WAV etc.
I got this to work using the HDR-UX1, using the -ar 44100 audio sample rate switch.
PCLinuxOS had ffmpeg perfectly setup for this operation (I didn't need to manually install it).
Worked like a dream. I used the Bit Rate calculator found here http://www.3ivx.com/support/calculator/index.html
ffmpeg -i test.mts -vcodec libxvid -b 15804k -acodec libmp3lame -ac 2 -ab 384k -deinterlace -s 1920x1080 test.avi
ffmpeg -i test.avi
Input #0, avi, from 'test.avi':
Metadata:
ISFT : Lavf52.71.0
Duration: 00:01:24.14, start: 0.000000, bitrate: 8294 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, 2 channels, s16, 320 kb/s
At least one output file must be specified
Thanks again for all your hard work,
Brian
Thanks!
This also works with Sony Handycam HDR-SR5E files which have an .m2ts suffix.
I shall be trying to crop my videos which have a pixel aspect ratio of 4:3 but a display asp. I will write again if I am successful to let you know how I got on.
Richard
Hi,
Thanks for the article, this cmd worked fine for me converting files from a Panasonic GF-1 into mpeg2 ready for burning onto DVD:
ffmpeg -i 00002.MTS -target pal-dvd video2.mpgtried
leleu@portable-leleu:~/Documents/m2ts$ libxvid -i input.m2ts -vcodec mpeg4 -b 18000k -acodec libmp3lame -ac 2 -ab 320k -deinterlace -s 1440x1080 output.avi
said :
Unknown encoder 'libxvid'
found that libxvidcore4-dev is no longer available under Lucid, libxvidcore-dev is the new package.....but the ffmpeg lucid package doesn't work with it.....and last ffmpeg can't be found, since, at least from France at the time, http://www.ffmpeg.org/index.html is unreachable.
tried
leleu@portable-leleu:~/Documents/m2ts$ ffmpeg -i input.m2ts -vcodec libxvid -b 18000k -acodec libmp3lame -ac 2 -ab 320k -deinterlace -s 1440x1080 output.avi
said :
Unknown encoder 'libmp3lame'
however libmp3lame-dev is installed.......
Thanks for this. I had to change
-vcodec libxvidto-vcodec mpeg4to get it work in mandrivalinux 2010.1 beta 1, but now it works perfectly for me.Thanks, worked great here. Sony HDC-HS250.
I am trying to convert MTS files from a Panasonic GH1. ffmpeg -i reveals:
----------------------------------
FFmpeg version SVN-r22608, Copyright (c) 2000-2010 the FFmpeg developers
built on Mar 20 2010 16:17:25 with gcc 4.4.1
configuration: --enable-gpl --enable-libmp3lame --enable-libxvid --enable-libfaac --enable-nonfree
libavutil 50.12. 0 / 50.12. 0
libavcodec 52.59. 0 / 52.59. 0
libavformat 52.56. 1 / 52.56. 1
libavdevice 52. 2. 0 / 52. 2. 0
libswscale 0.10. 0 / 0.10. 0
[mpegts @ 0x98993d0]MAX_READ_SIZE:5000000 reached
Seems stream 0 codec frame rate differs from container frame rate: 119.88 (120000/1001) -> 59.94 (60000/1001)
Input #0, mpegts, from '00019.MTS':
Duration: 00:00:21.00, start: 0.376611, bitrate: 15283 kb/s
Program 1
Stream #0.0[0x1011]: Video: h264, yuv420p, 1280x720 [PAR 1:1 DAR 16:9], 59.92 fps, 59.94 tbr, 90k tbn, 119.88 tbc
Stream #0.1[0x1100]: Audio: ac3, 48000 Hz, stereo, s16, 192 kb/s
At least one output file must be specified
------------------------------------
When i run the command
ffmpeg -i input.MTS -vcodec libxvid -b 18000k -acodec libmp3lame -ac 2 -ab 320k -deinterlace -s 1280x720 output.avi
I get a sloppy output, with dropped frames. Any help would be appreciated.
Neil
You have NTSC MTS file, (59.92 fps), use correct switch
This works fine with my Panasonic/Lumix GH1 and Fedora 12.
This seems to have worked well under windows as well... I have a Panasonic hdc-sd9.
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!
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?
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
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.
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
}
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
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
Precious !!!
Just what the doctor ordered. The joy of using Nautilus scripts.
Thank you so very much !!
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
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.
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.
works great using Panasonic HDC-SD9
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.
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
Super!
Works like a charm with .MTS files from my Canon Legria HF200E HD cam (also using the -deinterlace switch).
Thank you
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!!
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.
Thanks, works for me with Panasonic HDC-SD 100 on Kubuntu jaunty on a five year old desktop.
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
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"
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"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.
Post new comment