[offtopic] video encoding with mencoder of ffmpeg

Thread in 'Discussion' started by Burbruee, 30 Jun 2009.

  1. Hey,

    I've been busy the last couple of days redoing some parts of my video website.
    I use JW FLV Player to playback the videos, and usually I use a windows box to convert my videos and then transfer them back to the server when they're done, but I want to be able to do this server-side as well.

    I have some space where some users may upload videos, and I want to convert these to some H.264 in a mp4 container so the flash video player can play it.

    As a test I uploaded a video of myself playing some Texmaster.
    I tried making it into an flv with ffmpeg, and it worked out. But I really hate flv and you can get so much better quality with x264 for video and some aac codec for audio within an mp4 container...

    I'm not really sure how to do that though, I've spent an hour googling, but couldn't really find what I want, it seems as if mencoder can't output a real mp4 container.

    If someone here is experienced with mencoder/ffmpeg/mp4box etc and know how to make a flash compatible mp4 using x264, please post. :)
     
  2. I first got into video processing using the speeddemosarchive.com wiki tutorials. I believe the one relevant to your interests is here:

    http://speeddemosarchive.com/kb/index.php/MeGUI

    That's how I personally use it so if you have trouble setting it up just ask.
     
  3. That's exactly what I use as well. :D
    What I want to do though is converting on the server. (which is running linux and does not run anything graphical)
    Eventually, my final goal will be to make conversion automatic, but for now manual conversion is good enough.
    But I think I got it, found this bash script:
    Code:
    #!/bin/bash
    #
    ORIGDIR=`pwd`
    FILE=$@
    VID_OUTPUT=${FILE/.AVI/.tmp.avi}
    FINAL_OUTPUT=${FILE/.AVI/.mp4}
    MENCODER=~/src/mplayer/svn/mplayer/mencoder
    BITRATE=500
    RES=640:480
    LOG=mencoder.$FILE.log
    
    TEMPDIR=`mktemp -d /tmp/transcode.XXXXXXXX`
    pushd $TEMPDIR
    echo "Pass 1"
    
    PARAMS=keyint=250:bframes=0:qp_min=10:qp_max=51:turbo=1:mixed_refs=0:frameref=1:deblock=0,0:qblur=0:vbv_bufsize=10000:vbv_maxrate=10000:keyint_min=25:me=hex:me_range=16:ip_factor=1.4:pb_factor=1.3:chroma_qp_offset=0:vbv_init=0.9:ratetol=1.0:cplx_blur=20:nocabac:noweight_b:nob_pyramid:partitions=p8x8,b8x8,i4x4:no8x8dct:nossim:deadzone_inter=21:deadzone_intra=11
    
    EXTRA_PARAMS=subq=5:trellis=0:
    
    $MENCODER $ORIGDIR/$FILE -o $VID_OUTPUT -oac faac -srate 44100 -ovc x264 -x264encopts bitrate=$BITRATE:$PARAMS:pass=1 -vf scale=$RES 2>&1 >> $LOG
    echo "Pass 2"
    $MENCODER $ORIGDIR/$FILE -o $VID_OUTPUT -oac faac -srate 44100 -ovc x264 -x264encopts bitrate=$BITRATE:$PARAMS:pass=2 -vf scale=$RES 2>&1 >> $LOG
    MP4Box -aviraw video $VID_OUTPUT
    MP4Box -aviraw audio $VID_OUTPUT
    TEMP_AUDIO=${VID_OUTPUT/.avi/}_audio.raw
    AAC_AUDIO=${TEMP_AUDIO/.raw/.aac}
    H264_VIDEO=${VID_OUTPUT/.avi/}_video.h264
    echo TEMP is $TEMP_AUDIO
    echo AAC is $AAC_AUDIO
    echo H264 is $H264_VIDEO
    mv $TEMP_AUDIO $AAC_AUDIO
    MP4Box -add $H264_VIDEO:fps=29.97 -add $AAC_AUDIO $FINAL_OUTPUT
    mv $LOG $ORIGDIR
    mv $FINAL_OUTPUT $ORIGDIR
    popd
    echo Files left in $TEMPDIR
    # rm -fr $TEMPDIR
    I just need to compile MP4Box from the gpack project. (which just finished with success, however it doesn't work, apparently some error when loading shared library..)
     
  4. I have it working now, but not without problems. :)
    Even though I said output fps should be 60fps, it's played back at 25 fps or something. So it looks like my grandmother is playing Texmaster.
     
  5. Hm, a bit of a problem.
    This is a 320x240@60fps avi video from Texmaster and then encoded with above script, looks fine:
    http://143.homelinux.com/live/beta.php?id=25

    This is a 720x480@30fps avi I recorded uncompressed with my capture card, uploaded to my server and then encoded with the script:
    http://143.homelinux.com/live/beta.php?id=30

    It looks a bit funky.. I encoded it with a bitrate of 2500kbps yet it looks like that. I'm aware of the fact that it's interlaced, but it should still not look like that, also it looks the same even after deinterlacing.
    Using SDA's settings for MeGUI makes it look perfect, so it must be some encoding options I need to add.

    Any ideas of what might be wrong?
     
  6. I'm seeing lots of interlacing artifacts, but I guess you don't mean those. Aside from that it's blurry... What sort of quality does your capture card record? Some of it is probably caused by stretching due to incorrect resolution. You'll want to deinterlace it and resize to 640x480, assuming your capture card is getting the full picture.
     
  7. Compare to this which is also interlaced: (encoded with MeGUI)
    http://143.homelinux.com/live/beta.php?id=32

    EDIT: And here, deinterlaced with yadif:
    http://143.homelinux.com/live/beta.php?id=33

    My capture card does 720x576 or 720x480 depending on PAL/NTSC/PAL60 at 25 or 30 fps. Source is S-video so image is pretty sharp.

    The blur and the blocking artifacts is what's bothering me. It's what you'd expect to get if you encode with a low bitrate, but 2500kbps should give me pretty high quality. Not at all like that, it should look like the videos in this post.

    I'll toy with the params of my script, I see now that they are quite different from what I have in MeGUI.
    Code:
    program --pass 2 --bitrate 2048 --stats ".stats" --level 4.1 --ref 3 --mixed-refs --no-fast-pskip --bframes 3 --weightb --direct auto --subme 7 --trellis 2 --partitions p8x8,b8x8,i4x4,i8x8 --8x8dct --qpmin 19 --vbv-bufsize 9000 --vbv-maxrate 25000 --me umh --threads auto --thread-input --progress --no-psnr --no-ssim --output "output" "input" 
     
    Last edited: 30 Jun 2009
  8. Yeah, you can definitely rule out the source video and resolution being a problem. Unfortunately I'm more experienced with video processing than video encoding so I can't really help troubleshoot beyond this.
     
  9. Everything is working now, and I made it into a php script, I'll just modify it further to add upload forms that will insert stuff into the database and people should be able to upload videos for conversion.

    http://pastebin.com/f75e7e6a1

    *should* work.
     

Share This Page