.MTS (AVCHD/H.264) conversion?

*nix specific usage questions
Shadow_7
Blank Cone
Blank Cone
Posts: 14
Joined: 02 Sep 2008 02:39

.MTS (AVCHD/H.264) conversion?

Postby Shadow_7 » 01 Oct 2008 03:14

I'm looking to buy an HD camcorder. The Canon HG20 specifically. I went to best buy and made some samples on an SDHC flash drive (transcend 16GB class 6). I'm having trouble converting the largest clip (1GB / 5 minutes). I am able to convert the smaller clips just fine.

ffmpeg -i 00000.mts -sameq -target ntsc-dvd 00000_dvd.mpg
ffmpeg -i 00001.mts -sameq -target ntsc-dvd 00001_dvd.mpg

But on the larger clip I get severe artifacts after about the two or three minute mark with ffmpeg. I am able to play the entire media without too many artifacts with vlc. If I disable frame dropping and slow the playback down the 0.25x, I can see all of the frames recorded with minimal artifacts and about 90% cpu usage. My current laptop is too slow to play them at speed. I've learned that vlc can convert the media, so I've been trying to do that. However doing so yields an audio only output file for me. Any tips for converting my mts file to a playable / 720p or DVD version? The media appears to be Mpeg 2 TS aka 24Mbps AVCHD / H.264. Mplayer does not play them without specifying the frame rate -fps 5. Xine or Totem cannot play them. Debian stable / etch, with a lot of the x264, ffmpeg, xvid, vlc, mplayer, and others from source. In addition to debian-multimedia.org in the apt sources (recent addition).

I recorded the first two at 60i. I may have switched the 3rd to 30p, or 24p, but I'm not entirely sure. It appears to be 60i just like the others for the most part. 60fps interlaced, I assume. Actually I need to use the following mencoder line to convert to DVD 29.97 fps quality. Because of the frame rate.

mencoder -of mpeg -oac copy -mpegopts format=dvd:vaspect=16/9:vframerate=30 -fps 60000/1001 -ofps 30000/1001 -ovc lavc -lavcopts vcodec=mpeg2video:vrc_buf_size=1835:keyint=18:vrc_maxrate=9800:vbitrate=6000:aspect=16/9 -vf scale=720:480 00001.mts -o hg20_dvd1.mpeg


With VLC attempts like this one.

cvlc 00002.mts --sout '#transcode{vcodec=h264,vb=1024,acodec=mp4a,ab=224,channels=2,deinterlace,hq}:std{access=file,mux=mp4,dst=vlc_test.mp4}'

And this one, but some complaint on video encoder.

cvlc -vvv 00002.mts --sout '#transcode{vcodec=mp2v,vb=800,scale=1,acodec=mpga,ab=128,channels=2}:duplicate{dst=std{access=file,mux=mpeg,dst=vlc_test.mpg}}'

As I've said, these result in an audio only stream. As mplayer doesn't see a video stream. Even when vlc doesn't complain much. One other quirk, when I start vlc ($ vlc 00001.mts) it plays the video. But if I stop and start playback a second time, it doesn't show the video and just plays the audio.

My current goal is to make DVDs of my recordings. While hanging on to the HD / blueray originals. And possibly editing with 720p content on the laptop. I'm needing to record 15 minute minimums and if I can't get over the 3 minute-ish glitch, then obviously I wont be able to use this cam with it's video format. Any insight to get around this? Blatant syntax errors above? Missing libs / software? VLC 0.9.3 and 0.9.1. x264 snapshot(s) from source 9/01/2008 & 9/28/2008.

Jean-Baptiste Kempf
Site Administrator
Site Administrator
Posts: 37519
Joined: 22 Jul 2005 15:29
VLC version: 4.0.0-git
Operating System: Linux, Windows, Mac
Location: Cone, France
Contact:

Re: .MTS (AVCHD/H.264) conversion?

Postby Jean-Baptiste Kempf » 01 Oct 2008 04:01

Without messages, we cannot help.
Jean-Baptiste Kempf
http://www.jbkempf.com/ - http://www.jbkempf.com/blog/category/Videolan
VLC media player developer, VideoLAN President and Sites administrator
If you want an answer to your question, just be specific and precise. Don't use Private Messages.

Shadow_7
Blank Cone
Blank Cone
Posts: 14
Joined: 02 Sep 2008 02:39

Re: .MTS (AVCHD/H.264) conversion?

Postby Shadow_7 » 02 Oct 2008 16:09

I found a workaround of sorts.

ffmpeg -i 00002.mts -sameq -f mp4 -vcodec mpeg4 outfile.mp4

Although 60i becomes 30p without dropping any frames, so video is twice as long as the audio portion.

I upgraded to the latest ffmpeg snapshot (10/1). And vlc 0.9.3. From source. And added gpac (MP4Box) from source. And added a symlink. ln -s /usr/lib/liba52.so /usr/lib/liba52.so.0 Ffmpeg was complaining about it's absence, and I believe vlc was too, but I just didn't get that reference at the time.

cvlc 00002.mts --sout '#transcode{vcodec=h264,vb=1024,acodec=mp4a,ab=224,channels=2,deinterlace,hq}:std{access=file,mux=mp4,dst=vlc_test.mp4}'

Now outputs a file. But it's audio only, once again. No errors. When it finishes it sort of sits there. Until stopped with ^C.

$ cvlc 00002.mts --sout '#transcode{vcodec=h264,vb=1024,acodec=mp4a,ab=224,channels=2,deinterlace,hq}:std{access=file,mux=mp4,dst=vlc_2.mp4}'
VLC media player 0.9.3 Grishenko
[00000001] main libvlc debug: VLC media player - version 0.9.3 Grishenko - (c) 1996-2008 the VideoLAN team
[00000001] main libvlc debug: libvlc was configured with ./configure '--prefix=/usr' '--disable-dbus' '--disable-hal' '--enable-pvr' '--enable-v4l' '--enable-v4l2' '--enable-dvb' '--enable-faad' '--enable-twolame' '--enable-png' '--enable-x264' '--enable-svg' '--enable-snapshot' '--with-gnu-ld' '--with-pic' '--with-x' '--disable-live555' '--disable-qte' '--enable-growl'
[00000001] main libvlc debug: translation test: code is "C"
[00000418] dummy interface: using the dummy interface module...
libdvbpsi error (PSI decoder): TS discontinuity (received 1, expected 0) for PID 0
libdvbpsi error (PSI decoder): TS discontinuity (received 1, expected 0) for PID 256
[00000490] a52 packetizer: A/52 channels:2 samplerate:48000 bitrate:256000
No accelerated IMDCT transform found
[00000494] a52 decoder: A/52 channels:2 samplerate:48000 bitrate:256000
x264 [info]: using cpu capabilities: MMX2 SSE2Slow

[00000725] signals interface error: Caught Interrupt signal, exiting...

The last line results when I ^C out of the app. And the resultant file is about 0.75% of the original. Despite taking a while to create from a 5 minute 41 second clip. So most likely just audio. Of course I'm wanting to convert the video portion. Just not successful with vlc quite yet.

Shadow_7
Blank Cone
Blank Cone
Posts: 14
Joined: 02 Sep 2008 02:39

Re: .MTS (AVCHD/H.264) conversion?

Postby Shadow_7 » 02 Oct 2008 17:10

$ vlc 00002.mts

VLC media player 0.9.3 Grishenko
[00000001] main libvlc debug: VLC media player - version 0.9.3 Grishenko - (c) 1996-2008 the VideoLAN team
[00000001] main libvlc debug: libvlc was configured with ./configure '--prefix=/usr' '--disable-dbus' '--disable-hal' '--enable-pvr' '--enable-v4l' '--enable-v4l2' '--enable-dvb' '--enable-faad' '--enable-twolame' '--enable-png' '--enable-x264' '--enable-svg' '--enable-snapshot' '--with-gnu-ld' '--with-pic' '--with-x' '--disable-live555' '--disable-qte' '--enable-growl'
[00000001] main libvlc debug: translation test: code is "C"
[00000001] main libvlc: Running vlc with the default interface. Use 'cvlc' to use vlc without interface.
libdvbpsi error (PSI decoder): TS discontinuity (received 1, expected 0) for PID 0
libdvbpsi error (PSI decoder): TS discontinuity (received 1, expected 0) for PID 256
[00000492] a52 decoder: A/52 channels:2 samplerate:48000 bitrate:256000
ALSA lib pcm_dmix.c:1008:(snd_pcm_dmix_open) unable to open slave
[00000493] oss audio output error: cannot open audio device (/dev/dsp)
QPainter::begin: Paint device returned engine == 0, type: 1
QPainter::begin: Paint device returned engine == 0, type: 1
QPainter::begin: Paint device returned engine == 0, type: 1
[00000453] avcodec decoder error: more than 5 seconds of late video -> dropping frame (computer too slow ?)
libdvbpsi error (PSI decoder): TS discontinuity (received 1, expected 0) for PID 0
libdvbpsi error (PSI decoder): TS discontinuity (received 1, expected 0) for PID 256
[00000535] a52 packetizer: A/52 channels:2 samplerate:48000 bitrate:256000
No accelerated IMDCT transform found
[00000537] a52 decoder: A/52 channels:2 samplerate:48000 bitrate:256000
[00000557] avcodec encoder error: cannot open encoder
[00000519] stream_out_transcode stream out error: cannot find video encoder (module:any fourcc:mp2v)
[00000519] stream_out_transcode stream out error: cannot create video chain
[00000534] main packetizer error: cannot create packetizer output (h264)

$ vlc 00002.mts
then Video -> Zoom -> Half
then Stop
Then Media -> Convert / Save
Select source --- file.mts
Select File
Select destination --- file.mpg
Select Mpeg 2 from Profile / list box
:sout=#transcode{vcodec=mp2v,vb=800,scale=1,acodec=mpga,ab=128,channels=2}:duplicate{dst=std{access=file,mux=ts,dst=/mnt/hda4/dvd/hg20_experiment/vlc_2.mpg}}
Select Save

Streaming / Transcoding failed:
VLC could not open the encoder.

Shadow_7
Blank Cone
Blank Cone
Posts: 14
Joined: 02 Sep 2008 02:39

Re: .MTS (AVCHD/H.264) conversion?

Postby Shadow_7 » 03 Oct 2008 04:12

I installed the svn version 0.8.6 for debian (distro supplied) and it was able to save a video(cvlc ONLY). But the results took forever and were garbage past the top 5% of the picture. So I went for 0.8.6i from source. And it barked at having swscale enabled in ffmpeg. So I disabled it and it still barked. So I redid vlc 0.9.3 from source with the swscale still disabled. And it's no longer barking at the encoder. Although end results seem basically the same. Audio only, no video. With 0.8.6 from the distro I have video, but no audio. Not that the video converted looked anything like the source.

Shadow_7
Blank Cone
Blank Cone
Posts: 14
Joined: 02 Sep 2008 02:39

Re: .MTS (AVCHD/H.264) conversion?

Postby Shadow_7 » 08 Oct 2008 02:00

I guess I have a workflow now.

ffmpeg -i file.MTS -sameq -an -f yuv4mpegpipe - | yuvfps -s 60000:1001 -r 60000:1001 | ffmpeg -f yuv4mpegpipe -sameq -i - -sameq -s 1280x720 -f mpeg2video -r 60000/1001 outfile.MPEG

This gives me usable 60 fps video that I can edit and work with. Or rejoin the audio with. For some reason if I use ffmpeg directly, it always assumes that the MTS file is 30p.

ffmpeg -i file.MTS -vn outfile.WAV

ffmpeg -i infile.MPEG -i infile.WAV -sameq -target ntsc-dvd dvd.MPEG

I still don't now if the A/V sync is 100% that way. But it works. I'll try again with vlc 0.9.4 once I get some time to download / install it over dialup. But so far only vlc 0.8.6 tried to do the video conversion. And wasn't that great at it.

Shadow_7
Blank Cone
Blank Cone
Posts: 14
Joined: 02 Sep 2008 02:39

Re: .MTS (AVCHD/H.264) conversion?

Postby Shadow_7 » 09 Oct 2008 05:28

vlc 0.9.4 - audio only output for mpeg-2 and mpeg4/divX. Mpeg-2 complained about not finding the encoder. Mpeg4/xviD didn't, but still audio only results.

Shadow_7
Blank Cone
Blank Cone
Posts: 14
Joined: 02 Sep 2008 02:39

Re: .MTS (AVCHD/H.264) conversion?

Postby Shadow_7 » 14 Nov 2008 00:05

It's been some time so I guess I'll give an update. It appears that AVCHD is not correctly implemented in any of the linux routes. Not that I would know what is correct since I only run linux. But I now have basically three ways to convert AVCHD 60i footage in linux. Thanks to many sources. The basic problem is that A/V sync is not maintained as exact number of frames vs. exact length of the audio never quite gel. And various other motion / frame rate conversion issues. This appears to mainly be an issue with interlaced footage. Particularly with the newer 24Mbps capable AVCHD camcorders.

With plain old ffmpeg 60i is being interpreted as 30p, making the video roughly twice as long as the audio. With two various ways to overcome this tendency. One way is piping a yuv4mpegpipe from ffmpeg to yuvfps back to ffmpeg. Another is piping rawvideo from ffmpeg to ffmpeg. All three ways requires audio to be handled separately and rejoined later. VLC tries to play it, but my computers are too slow to do that in realtime. And VLC seems to not correctly re-encode the video to other formats. Most times resulting in audio only(0.9.2 / 0.9.4), or video more closely resembling a screen saver (0.8.6). On to the three ways.

My first success story of sorts. But apparently A/V sync issues. I should note that ffmpeg defaults to 25fps if you don't override the default. Anyway, through yuvfps.

$ ffmpeg -i 00000.mts -an -sameq -an -deinterlace -f yuv4mpegpipe - | yuvfps -v 0 -s 60000:1001 -r 60000:1001 | ffmpeg -f yuv4mpegpipe -i - -sameq -aspect 16:9 -qmin 4 -f avi -vcodec msmpeg4v2 -r 60000/1001 output_video_only.mp4

My second success and the fastest route so far. But apparently A/V sync issues as well. Some m2ts type files seem to favor 59.4 rates (6000/101) instead of 59.94 (60000/1001) fps settings.

$ ffmpeg -i 00001.mts -an -pix_fmt yuv420p -s 1920x1080 -f rawvideo - | ffmpeg -pix_fmt yuv420p -s1920x1080 -r 60000/1001 -f rawvideo -i - -sameq -aspect 16:9 -qmin 4 -f avi -vcodec msmpeg4v2 -r 60000/1001 output_video_only.mp4

And some cross platform tools that seem a little more accurate for certain mts files, but not others.

$ xport -h 00002.mts 1 1 1
$ ldecod -i bits0001.mpv -o bits0001.yuv
$ mv bits0001.mpa bits0001.ac3
(and either this)
$ yuv4mpeg -w 1920 -h 1080 -x 420mpeg2 < bits0001.yuv | ffmpeg -i - -sameq -aspect 16:9 -qmin 4 -f avi -vcodec msmpeg4v2 -r 60000/1001 output_video_only.mp4
(or that)
$ ffmpeg -s 1920x1080 -r 60000/1001 -pix_fmt yuv420p -f rawvideo -i bits0001.yuv -f yuv4mpegpipe - | ffmpeg -f yuv4mpegpipe -i - -sameq -aspect 16:9 -qmin 4 -f avi -vcodec msmpeg4v2 -r 60000/1001 output_video_only.mp4

The advantage of the yuv4mpegpipe lets you throw in some mjpegtools processing to aid the image quality. yuvdenoise, yuvscaler, and others. But each additional process makes the conversion that much slower. With the first one, it takes about 4 to 6 times the length of the footage to convert. With the second one it takes about 2 to 4 times the footage. With the last one it takes about 15 times the footage with 14.2 version of ldecod. About 30 times the footage with 12.2 version of ldecod. I should note that they're windows executables so they(xport/ldecod) launch wine when run, if you place them in /usr/bin/. From various sources to come up with the above.

For the yuvfps hint:
http://www.linuxjournal.com/video/linux ... gic-ffmpeg

For the rawvideo hint I consulted the ffmpeg-users mailing list.

For the xport / ldecod stuff:
http://www.avsforum.com/avs-vb/showthread.php?t=789775

And don't forget to rejoin the audio.
$ ffmpeg -i video_file -i audio_file -vcodec copy -acodec copy finalfile.format

Jean-Baptiste Kempf
Site Administrator
Site Administrator
Posts: 37519
Joined: 22 Jul 2005 15:29
VLC version: 4.0.0-git
Operating System: Linux, Windows, Mac
Location: Cone, France
Contact:

Re: .MTS (AVCHD/H.264) conversion?

Postby Jean-Baptiste Kempf » 14 Nov 2008 08:16

Thanks for those valuable input.
Jean-Baptiste Kempf
http://www.jbkempf.com/ - http://www.jbkempf.com/blog/category/Videolan
VLC media player developer, VideoLAN President and Sites administrator
If you want an answer to your question, just be specific and precise. Don't use Private Messages.

HDGeorge
New Cone
New Cone
Posts: 1
Joined: 25 Aug 2009 04:49

Re: .MTS (AVCHD/H.264) conversion?

Postby HDGeorge » 25 Aug 2009 05:41

It seems to me as if the process explained in the previous posts is rather complex and thus possibly error prone. I recently tried HD conversion with ffmpeg and have the impression, that it is essential to concatenate the MTS files that were recorded in one go (i.e. the files that were created between "start recording" and "stop recording" on the camcorder). As they are essentially dumped streams and cut to size (usually <2GB) by the camcorder, you get into trouble if you try to convert an MTS file that starts somewhere in the middle of a recording session, as it references B and I frames from the previous MTS file. If you feed such an MTS file into ffmpeg, then ffmpeg will display something like this:

Code: Select all

[h264 @ 0x273aaf0]number of reference frames exceeds max (probably corrupt input), discarding one Last message repeated 2 times
As there are frames dropped, you run into audio synch issues sooner or later. To get around it, you have to join the files, preferably virtually as they are pretty huge to start with. I wrote the following script that deinterlaces (from "i60" to "p30") and joins MTS files and produces one quality h264 encoded avi as output (essentially leaves the audio untouched for maximum quality):

Code: Select all

#!/bin/bash if [ $# -lt 1 ] ; then echo "expected at least one input file" exit 1 fi # Create FIFO to allow input from multiple MTS files at once TMPFILE=`mktemp /tmp/mts.XXXXXX` || exit 1 rm $TMPFILE mkfifo $TMPFILE # Create tmp file for cat command CATTMP=`mktemp /tmp/cat.XXXXXX` || exit 1 # set output filename to concatenation of input MTS streams # and build cat script as input to fifo MTS="`basename "$1"`" MTS="`echo "$MTS" | sed s,\.MTS,,g`" FILENAME="$MTS" echo "cat \"$1\" \\" > $CATTMP COUNTER=1 ARGS=("$@") while [ "$COUNTER" -lt "$#" ] ; do echo "\"${ARGS[$COUNTER]}\" \\" >> $CATTMP MTS="`basename "${ARGS[$COUNTER]}"`" MTS="`echo "$MTS" | sed s,\.MTS,,g`" FILENAME="${FILENAME}-${MTS}" COUNTER=$(($COUNTER + 1)) done echo "> $TMPFILE &" >> $CATTMP chmod +x $CATTMP FILENAME="${FILENAME}.avi" # Concatenate MTS streams in TMPFILE / execute CATTMP $CATTMP # Encode with ffmpeg nice -n 19 ffmpeg \ -i "$TMPFILE" \ -acodec copy \ -vcodec libx264 \ -crf 22 \ -r 30000/1001 \ -deinterlace \ -vpre lossless_medium \ -y \ -threads 0 \ "$FILENAME" # delete temporary files/fifos rm $TMPFILE rm $CATTMP
To use the script, just create a file in $HOME/bin/mts2avi.sh and put the contents into it. Then make it executable with chmod +x $HOME/bin/mts2avi.sh and run it like this

Code: Select all

mts2avi.sh /path/to/00021.MTS /path/to/00022.MTS
it will then create a file name 00021-00022.avi in the current directory.

You may want to play with the -vpre settings, all available settings are usually in /usr/share/ffmpeg. You may need a recent ffmpeg and x264 version. Instructions how to compile these on Ubuntu are herehttp://ubuntuforums.org/showthread.php?t=786095


Return to “VLC media player for Linux and friends Troubleshooting”

Who is online

Users browsing this forum: No registered users and 2 guests