Fast PAL HD Video encoding in Sync with ffmpeg-svn from AVHCD to Mpeg4 under Linux

I have actually found a very very nice solution to the high definition video(AVCHD) transcoding problem. I got the ffmpeg svn checkout from yesterday (21.08.2008) and it works great with this script, all in one. No need for extra tools. And it's quite speedy.


#/bin/sh

FFM=$HOME/src/ffmpeg/ffmpeg
BR=3000 # kbit
OUT="${1}.mkv"

[ -f "$OUT" ] && exit 1;

echo $1
# old ffmpeg versions (2008):
nice $FFM -r 50 -i "$1" -s 1280x540 -ilme mpeg4 -acodec copy -aspect 16:9 -b ${BR}k "$OUT"
# new ffmpeg versions (January 2009): -r 50 might still be necessary.
nice $FFM -i "$1" -s 1280x540 -flags ilme -flags ildct -vcodec mpeg4 -acodec copy -aspect 16:9 -b ${BR}k "$OUT"


The only problem is that the current ffmpeg checkout does not listen to the -r parameter. So you have to manually override the frames per second when playing the file, e.g. mplayer -fps 50. I use mplayer -fps 50 -fs -af volnorm -vf pp=fd.

You need might need to adjust the path to ffmpeg in the script obviously, e.g. to $HOME/ffmpeg/ffmpeg.

Of course if you have a camera that records 29.97 frames per second you have to change the numbers accordingly. (-r 59.94)

If you've got several cores you may want to experiment with the -threads option and make sure you have compiled with --enable-pthreads, see below.

The great thing is that the video is encoded quickly - I get 23 fps on my celeron m530 notebook - and the video is perfectly in sync, and you only need ffmpeg, nothing else.

You should install a fresh ffmpeg from SVN:

svn checkout svn://svn.mplayerhq.hu/ffmpeg/trunk ffmpeg
cd ffmpeg
./configure --enable-nonfree --enable-gpl --enable-pthreads && make && sudo make install.


Be careful where you put every ffmpeg parameter, as ffmpeg interprets them differently depending on the order you give them. E.g. ffmpeg -r 50 -i ... differs from ffmpeg -i ... -r 50.

Enjoy fast and sync video transformation!

If it doesn't work for you, try out this script, which is the result of the long discussions below.

30 comments:

  1. I get an empty file with this script!

    ReplyDelete
  2. That shouldn't be. What ffmpeg version do you use?

    ReplyDelete
  3. btw. there's now VDPAU support in ffmpeg: http://www.phoronix.com/scan.php?page=news_item&px=Njk3MQ

    I wonder when that accelerates the process even further...

    ReplyDelete
  4. I have the latest ffmpeg-svn, or should I say three days old. I have ffmpeg in my home folder, also a bin-folder that has the script. The script looks like this

    #/bin/sh

    FFM=/home/myname/ffmpeg
    BR=3000 # kbit
    OUT="${1}.mkv"

    [ -f "$OUT" ] && exit 1;

    echo $1
    nice $FFM -r 50 -i "$1" -s 1280x540 -ilme mpeg4 -acodec copy -aspect 16:9 -b ${BR}k "$OUT"

    The script is called "script", and when I run it, like "script 00002.mts", it turns the mts.file to a file that looks like a video file, but it's always 0 bytes.

    The media file is 1920 x 1080, 25 frames per second.

    So, any ideas?

    ReplyDelete
  5. If there are no error messages then I don't know either. It could be that the path to the ffmpeg binary is wrong, as it would usually be /home/myname/ffmpeg/ffmpeg.
    You could also try a different release.

    ReplyDelete
  6. I made a correction to the path, it's now ffmpeg/ffmpeg instead of just ffmpeg. Now, this is what I get:

    script Desktop/Kuvia/00001.MTS
    Desktop/Kuvia/00001.MTS
    FFmpeg version SVN-r16447, Copyright (c) 2000-2009 Fabrice Bellard, et al.
    configuration:
    libavutil 49.12. 0 / 49.12. 0
    libavcodec 52.10. 0 / 52.10. 0
    libavformat 52.23. 1 / 52.23. 1
    libavdevice 52. 1. 0 / 52. 1. 0
    built on Jan 6 2009 09:36:42, gcc: 4.3.2
    Input #0, mpegts, from 'Desktop/Kuvia/00001.MTS':
    Duration: 00:00:45.60, start: 0.374111, bitrate: 16496 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: 0x0000, 48000 Hz, 5.1, s16, 384 kb/s
    Invalid value 'mpeg4' for option 'ilme'

    ReplyDelete
  7. You may try to add a "-vcodec" in front of mpeg4:
    (...) -ilme -vcodec mpeg4 (...)

    ReplyDelete
  8. FFmpeg version SVN-r16447, Copyright (c) 2000-2009 Fabrice Bellard, et al.
    configuration:
    libavutil 49.12. 0 / 49.12. 0
    libavcodec 52.10. 0 / 52.10. 0
    libavformat 52.23. 1 / 52.23. 1
    libavdevice 52. 1. 0 / 52. 1. 0
    built on Jan 6 2009 09:36:42, gcc: 4.3.2
    Input #0, mpegts, from 'Desktop/Kuvia/00001.MTS':
    Duration: 00:00:45.60, start: 0.374111, bitrate: 16496 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: 0x0000, 48000 Hz, 5.1, s16, 384 kb/s
    Invalid value '-vcodec' for option 'ilme'

    Hmm, what I'm doing wrong?

    ReplyDelete
  9. Okay. You can
    1. Try completely without -ilme. That really should work. But if the source is interlaced, the quality per bitrate will suffer.

    2. Try to svn update, recompile and try again with the script from above. There might be a problem in the version you've checked out.

    Let me know how it goes. I'm sure we'll figure it out. If the things above don't work we should probably meet in IRC sometime to make this quicker.

    ReplyDelete
  10. Ok, some progress:

    I checked the latest svn.

    The script (last line, everything else is unchanged) line looks like this:
    nice $FFM -r 50 -i "$1" -s 1280x540 -vcodec mpeg4 -acodec copy -aspect 16:9 -b ${BR}k "$OUT"

    Then:

    Desktop/Kuvia/00001.MTS
    FFmpeg version SVN-r16564, Copyright (c) 2000-2009 Fabrice Bellard, et al.
    configuration:
    libavutil 49.12. 0 / 49.12. 0
    libavcodec 52.10. 0 / 52.10. 0
    libavformat 52.23. 1 / 52.23. 1
    libavdevice 52. 1. 0 / 52. 1. 0
    built on Jan 12 2009 16:42:41, gcc: 4.3.2
    Input #0, mpegts, from 'Desktop/Kuvia/00001.MTS':
    Duration: 00:00:45.60, start: 0.374111, bitrate: 16496 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: 0x0000, 48000 Hz, 5.1, s16, 384 kb/s
    Output #0, matroska, to 'Desktop/Kuvia/00001.MTS.mkv':
    Stream #0.0: Video: mpeg4, yuv420p, 1280x540 [PAR 3:4 DAR 16:9], q=2-31, 3000 kb/s, 25.00 tb(c)
    Stream #0.1: Audio: ac3, 48000 Hz, 5.1, s16, 384 kb/s
    Stream mapping:
    Stream #0.0 -> #0.0
    Stream #0.1 -> #0.1
    Press [q] to stop encoding
    [h264 @ 0x182ca70]B picture before any references, skipping
    [h264 @ 0x182ca70]decode_slice_header error
    [h264 @ 0x182ca70]no frame!
    Error while decoding stream #0.0
    [h264 @ 0x182ca70]B picture before any references, skipping
    [h264 @ 0x182ca70]decode_slice_header error
    [h264 @ 0x182ca70]no frame!
    Error while decoding stream #0.0
    frame= 2275 fps= 6 q=4.2 Lsize= 35731kB time=90.99 bitrate=3216.9kbits/s
    video:33562kB audio:2139kB global headers:0kB muxing overhead 0.084548%

    I got a matroska-video, that is about 50% of the original size. Ok. But... Audio is ok, but video is sorta slowmotion. MPlayer is giving some error, I don't remember what it was. VLC (nightly build) plays it without error, but video is slowmotion.

    Aaargh! What now? I feel I'm close to something, but something is wrong...

    ReplyDelete
  11. Good. That's more than before. You can try to add another -r 50 before the "$OUT".
    You can try to start mplayer with -fps 50.

    btw. If you copy this section:
    Input #0, mpegts, from 'Desktop/Kuvia/00001.MTS':
    Duration: 00:00:45.60, start: 0.374111, bitrate: 16496 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: 0x0000, 48000 Hz, 5.1, s16, 384 kb/s
    Output #0, matroska, to 'Desktop/Kuvia/00001.MTS.mkv':
    Stream #0.0: Video: mpeg4, yuv420p, 1280x540 [PAR 3:4 DAR 16:9], q=2-31, 3000 kb/s, 25.00 tb(c)
    Stream #0.1: Audio: ac3, 48000 Hz, 5.1, s16, 384 kb/s
    Stream mapping:
    Stream #0.0 -> #0.0
    Stream #0.1 -> #0.1
    Press [q] to stop encoding

    that should be enough. Do you have IRC? I'll be in #linux-tipps on freenode.

    ReplyDelete
  12. I will have to retest and maybe modify the script. I will post the results here when I'm finished.

    ReplyDelete
  13. I really appreciate your help, this is the only tool (so far) that I can use in my laptop. Other tools (Handbrage, that one .fifo-script) are melting my CPU or taking literally forever to convert my videos.

    ReplyDelete
  14. You could try removing the "-r 50". That would use ffmpeg's automatic framerate detection.

    ReplyDelete
  15. btw. Does the video play fine with ffplay? (ffmpeg/ffplay X.m2ts)

    ReplyDelete
  16. I've updated the installation instructions. Make sure you configure ffmpeg the right way. THat might make a difference.

    ReplyDelete
  17. ffplay plays it also out of A/V sync!!! I mean the original .MTS-file. So... Hmm, what's this all about?

    ReplyDelete
  18. Try to look at what the top command says during ffplay. Maybe (one of) your CPUs is used near 100%? You could then try ffplay -threads 2

    ReplyDelete
  19. If that doesn't help you may want to upload a sample and file a bug with ffmpeg: http://ffmpeg.mplayerhq.hu/bugreports.html

    ReplyDelete
  20. You could try different switches with ffplay: -sync (audio/video/ext) or -fast and/or -skipframe, -lowres, -threads 2

    ReplyDelete
  21. I was not able to sync the video with your tips, thanks however. I was wondering why the resolution is 1280x540? I tried it with 1280x720. MPLayer gives this output:
    VDec: vo config request - 1280 x 720 (preferred colorspace: Planar YV12)
    VDec: using Planar YV12 as output csp (no 0)
    Movie-Aspect is 1.78:1 - prescaling to correct movie aspect.
    VO: [xv] 1280x720 => 1280x720 Planar YV12
    A: 90.5 V: 90.9 A-V: -0.460 ct: 0.362 2274/2274 36% 2% 1.7% 5 0
    GNOME screensaver enabled

    Whats that 90.5 and 90.9? Related to sync somehow?

    Hmm, I guess it's not normal for videos to be out of sync in original form?

    Well, any tips and help still appreciated..

    ReplyDelete
  22. You're welcome!

    I've asked the ffmpeg developers and they said it'd be nice if you could post a bug and upload a sample. The link above explains how to do that.

    You may also want to try different videos and see if the results are the same.

    Unfortunately the Avchd support is not very far yet...

    Good Luck!
    Any Questions, let me know.

    ReplyDelete
  23. Hmm, where is the link for posting bug/file? I'll post right away when I see it! (The link might be so close I can't see it... Hah)

    ReplyDelete
  24. heres how you can upload a sample and file a bug with ffmpeg:

    -> http://ffmpeg.mplayerhq.hu/bugreports.html <-

    ReplyDelete
  25. Ok, my search is over..

    Codecs from
    http://www.mplayerhq.hu/design7/dload.html

    Then

    svn checkout svn://svn.mplayerhq.hu/mplayer/trunk mplayer
    ./configure
    make
    make install

    Then

    mencoder 00001.MTS -o 1.avi -oac copy -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=10000 -fps 50 -vf scale=1280:720

    I'm finally able to view my films!

    ReplyDelete
  26. Good to hear! I've posted a script with mencoder somewhere here, too, I think. It's much easier to handle, too. And you are sure the codec package was necessary? Can you post me the output of mencoder?

    ReplyDelete
  27. I didn't download codecs, so I guess you don't need'em!? Here is the output of a short clip, I removed some info in the middle, it looked just the same, so only the essential output is here.

    MEncoder dev-SVN-r28342-4.3.2 (C) 2000-2009 MPlayer Team
    CPU: AMD Turion(tm) X2 Dual-Core Mobile RM-70 (Family: 17, Model: 3, Stepping: 1)
    CPUflags: Type: 17 MMX: 1 MMX2: 1 3DNow: 1 3DNow2: 1 SSE: 1 SSE2: 1
    Compiled for x86 CPU with extensions: MMX MMX2 3DNow 3DNowEx SSE SSE2

    success: format: 0 data: 0x0 - 0x42c000
    TS file format detected.
    VIDEO H264(pid=4113) AUDIO A52(pid=4352) NO SUBS (yet)! PROGRAM N. 1
    FPS seems to be: 25.000000
    [V] filefmt:29 fourcc:0x10000005 size:0x0 fps:25.000 ftime:=0.0400
    Input fps will be interpreted as 50.000 instead.
    ==========================================================================
    Opening audio decoder: [liba52] AC3 decoding with liba52
    Using SSE optimized IMDCT transform
    Using MMX optimized resampler
    AUDIO: 48000 Hz, 2 ch, s16le, 384.0 kbit/25.00% (ratio: 48000->192000)
    Selected audio codec: [a52] afm: liba52 (AC3-liba52)
    ==========================================================================
    Opening video filter: [expand osd=1]
    Expand: -1 x -1, -1 ; -1, osd: 1, aspect: 0.000000, round: 1
    Opening video filter: [scale w=1280 h=720]
    ==========================================================================
    Opening video decoder: [ffmpeg] FFmpeg's libavcodec codec family
    Selected video codec: [ffh264] vfm: ffmpeg (FFmpeg H.264)
    ==========================================================================
    audiocodec: framecopy (format=2000 chans=2 rate=48000 bits=16 B/s=48000 sample-1)
    VDec: vo config request - 1920 x 1080 (preferred colorspace: Planar YV12)
    VDec: using Planar YV12 as output csp (no 0)
    Movie-Aspect is 1.78:1 - prescaling to correct movie aspect.
    SwScaler: reducing / aligning filtersize 7 -> 8
    SwScaler: reducing / aligning filtersize 7 -> 8
    SwScaler: reducing / aligning filtersize 7 -> 6
    SwScaler: reducing / aligning filtersize 7 -> 6
    [swscaler @ 0x2be9af0]BICUBIC scaler, from yuv420p to yuv420p using MMX2
    [swscaler @ 0x2be9af0]using 8-tap MMX scaler for horizontal luminance scaling
    [swscaler @ 0x2be9af0]using 8-tap MMX scaler for horizontal chrominance scaling
    [swscaler @ 0x2be9af0]using n-tap MMX scaler for vertical scaling (YV12 like)
    [swscaler @ 0x2be9af0]1920x1080 -> 1280x720
    videocodec: libavcodec (1280x720 fourcc=34504d46 [FMP4])
    Writing header...
    ODML: Aspect information not (yet?) available or unspecified, not writing vprp header.
    Writing header...
    ODML: Aspect information not (yet?) available or unspecified, not writing vprp header.
    [h264 @ 0x2857da0]B picture before any references, skipping0.000 [0:0]
    [h264 @ 0x2857da0]decode_slice_header error
    [h264 @ 0x2857da0]no frame!
    Error while decoding frame!
    [h264 @ 0x2857da0]B picture before any references, skipping0.002 [0:0]
    [h264 @ 0x2857da0]decode_slice_header error
    [h264 @ 0x2857da0]no frame!
    Error while decoding frame!

    1 duplicate frame(s)!
    Pos: 0.0s 3f (48%) 0.00fps Trem: 0min 0mb A-V:0.000 [0:0]
    1 duplicate frame(s)!
    Pos: 0.1s 4f (48%) 0.00fps Trem: 0min 0mb A-V:-0.001 [0:0]
    1 duplicate frame(s)!
    Pos: 2.3s 59f (100%) 13.48fps Trem: 0min 1mb A-V:-0.030 [4197:384]
    Flushing video frames.
    Writing index...
    Writing header...
    ODML: Aspect information not (yet?) available or unspecified, not writing vprp header.

    Video stream: 4197.628 kbit/s (524703 B/s) size: 1185830 bytes 2.260 secs 59 frames

    Audio stream: 384.000 kbit/s (48000 B/s) size: 115200 bytes 2.400 secs

    ReplyDelete
  28. Thanks for the script posted on the ubuntu forum; it just works !

    ReplyDelete
  29. You're welcome! Oh, where on the forum is it posted? I wasn't aware of that.

    ReplyDelete
  30. Thise one?
    http://ubuntuforums.org/showthread.php?t=1045153

    ReplyDelete

I appreciate comments. And I do read them.