Mplayer mencoder converting videos to flv files

The website I worked on is a casual game and video type website. Lately I have got a chance to revisit the back end media conversion engine we built half year ago. Instead of using ffmpeg, this time I used mplayer project to do the trick.

I like mencode better. FFmpeg is good, does everything, except did not give me enough options, switches to adjust. I did not find a middle way to have the quality and the size of the flv meet our needs.

In our environment, I have faac, faad2, lame installed. Faac and faad2 are a little trouble-some. I downloaded their release first, the source code just would not compile. So I tried their cvs version. It worked perfectly. I think I was too cautious.

Here is the mencode command I used:
#mencoder $1 -o $2 -ofps 15 -of lavf \
-oac mp3lame -lameopts abr:br=64 -srate 22050 -ovc lavc \
-lavfopts i_certify_that_my_video_stream_does_not_use_b_frames \
-lavcopts vcodec=flv:keyint=25:vbitrate=300:mbd=2:mv0\
:trell:v4mv:cbp:last_pred=3 \
-vf harddup,expand=:::::4/3,scale=320:240 \
-msglevel all=4 -mc 0/10
Those options are all doing something for us.

I had some result video sound out of sync problem first. The filter: harddup solved that problem. Then another file had sound out of sync half second, so I put the -mc 0/10. That's solved.

I had the video aspect sets to 4/3, then scaled to 320:240, that's what the -vf filters about. It's going to add black borders to the top/bottom or left/right accordingly.

The -lavcopts options are the longest and too much to explain. Try to find explanation from mplayer's website. mplayer site These options are switchs for qualities, frame rate and some other stuff. Spend some time on mplayer's document, and make your choices. It worths.

I was really worried about the gcc version of our website. It was between 3.1 and 3.2, the gcc version not recommended by the mplayer guys. Suppressingly, everything was fine.

Here is more detailed installations.
I had Lame 3.97. Downloaded from
#tar -zxvf lame-3.97.tar.gz
#cd lame
#su -c "make install"
Then had faac and faad2 downloaded from
#tar -zxvf faac-18102004.tar.gz
#cd faac
#chmod a+x bootstrap
#make && su -c "make install"
Faac installation should be trouble free. But I had to change a couple faad2 .h and .c files. The errors are:
mp4ff_int_types.h:23:7: warning: no newline at end of file
mp4ffint.h:373:7: warning: no newline at end of file
mp4ff.c:104: error: static declaration of ‘mp4ff_track_add’ follows non-static declaration
mp4ffint.h:348: error: previous declaration of ‘mp4ff_track_add’ was here
So I added a new line to the end of ./common/mp4ff/mp4ffint.h and mp4ff_int_types.h files. For file mp4ff.c, removed "static" at line 104:
static void mp4ff_track_add(mp4ff_t *f)
Make again, everything was okay. Then use command:
su -c "make install"
So far we had faad2, faac, lame installed. Before we download mplayer, a couple link need to be created.
#ln -s /usr/local/lib/ /usr/lib/
#ln -s /usr/local/lib/ /usr/lib/
#ln -s /usr/local/lib/ /usr/lib/
These links were created for mplayer. Mplayer is going to look for these libs at /usr/lib/ by default. There are config options to switch to /usr/local/lib. Although I did it in this way. There are cleaner solutions. Eather install faac,faad2, and lame into the /usr/lib/ or config mplayer to look for them at /usr/local/lib. Please let me know your solutions if you have this done.

Mplayer installation should be easy. Only mistake I made is I downloaded the windows codec package instead of linux. To have all codecs supported, I had the downloaded from mplayer site.
#bunzip2 all-20061022.tar.bz2
#tar -xvf all-20061022.tar.bz2
#su -c "mv all-20061022 /usr/local/lib/codecs"
The /usr/local/lib/codecs is a relative new place for these dll files. They used to be somewhere else. At this point mplayer is ready to be configured and made. Run following command in mplayer source directory.
I used the svn version of mplayer.
#svn checkout svn:// mplayer
#cd mplayer
#su -c "make install"
Everything should be working now. I haven't turn on a lot of things for mplayer, we don't need most of them, we don't play videos on screen anyways. It runs on a set of RHEL3 servers.

Here is another trick worth to mention. I need to take screenshots for uploaded videos. FFMpeg was used in our last version. Since I have switched to mplayer, to use ffmpeg for screenshots make me feel this job is unfinished. I did try to find a way for screenshots by using mencoder. It seems not able to. I have to turn to mplayer and here is the command for screenshots:

#mplayer \
-vf framestep=${4},scale=${2} -framedrop -nosound ${1} \
-speed 100 -vo jpeg:outdir=${3}
$4 is how many frames to skip for each screenshot.
$2 is a string looks like this: 320:240. It gives width:height information for the created jpeg files.
$1 is the media file.
-vo jpeg:outdir=$3 tells mplayer create jpeg files instead of playing it on the screen. :outdir=$3 is the output directory of those jpeg files.
-speed 100 is playing in a 100 times faster speed. So you don't have to wait for those screenshots for hours.

Beside all that, generated flv files has no meta information yet, FLVTool2 is what you need. This post is super large already. Let's give it a break.
1 comment

Popular posts from this blog

Spring framework, Hibernate and MySQL Replication

Testing Gaia GPS on iPhone 7plus on 5040 peak