`

在CentOS 6中使用mencoder/ffmpeg进行视频转码

阅读更多

MPlayer http://www.mplayerhq.hu/

FFmpeg http://ffmpeg.org/

 

使用mencoder转码

安装mplayer和mencoder

# yum install mplayer

 

RPMForge Repository Dependencies Resolved:

===============================================================

 Package                       Arch     Version                                    Repository    Size
===============================================================

Installing:                           
 mplayer                       x86_64   1.0-0.48.svn20100703.el6.rf   rpmforge     4.0 M
Installing for dependencies:          
 mplayer-common        x86_64   1.0-0.48.svn20100703.el6.rf   rpmforge     1.3 M
 mplayer-fonts              noarch    1.1-3.0.rf                                 rpmforge     1.0 M

 

RPM Fusion Dependencies Resolved:

===================================================================

 Package          Arch      Version                                      Repository                      Size
===================================================================

Installing:
 mplayer          x86_64   1.0-0.140.20120205svn.el6.1   rpmfusion-free-updates   1.1 M
Installing for dependencies:
 mplayer-common   x86_64   1.0-0.140.20120205svn.el6.1   rpmfusion-free-updates   1.2 M

 

# yum install mencoder

 

RPMForge Repository Dependencies Resolved:

==============================================================

 Package                      Arch         Version                                   Repository    Size
==============================================================

Installing:                                                                     
 mencoder                     x86_64   1.0-0.48.svn20100703.el6.rf   rpmforge     3.5 M

 

 RPM Fusion Dependencies Resolved:

===================================================================

 Package     Arch          Version                                         Repository                      Size
===================================================================

Installing:
 mencoder    x86_64    1.0-0.140.20120205svn.el6.1      rpmfusion-free-updates    829 k 

 

查看mencoder版本(官网最新版为4.8)

RPMForge Repository:

$ mencoder
MEncoder SVN-r31628-4.4.6 (C) 2000-2010 MPlayer Team

 

RPM Fusion:

$ mencoder
MEncoder SVN-r34652-4.4.6 (C) 2000-2012 MPlayer Team

 

将wmv转换为flv

$ mencoder -ovc lavc -lavcopts vcodec=flv:vbitrate=500:mbd=2:last_pred=3:cmp=3:trell:v4mv:cbp:qprd:mv0 -oac mp3lame -lameopts aq=3:preset=64 -lavdopts er=2 -pp 6 -of lavf -srate 44100 -ofps 12 -mc 0 ~/Videos/Wildlife.wmv -o ~/Videos/Wildlife.flv

 

输出:

...

Skipping frame!
Pos:  30.1s    900f (100%) 27.15fps Trem:   0min   2mb  A-V:0.000 [688:63]
Flushing video frames.
Writing index...

Video stream:  688.359 kbit/s  (86044 B/s)  size: 2588516 bytes  30.083 secs  900 frames

Audio stream:   63.874 kbit/s  (7984 B/s)  size: 239852 bytes  30.041 secs
 

播放转码后的视频

$ mplayer /boke/Wildlife.flv

使用RPMForge安装的能正常转码和播放,使用RPM Fusion安装的播放时只有声音。

 

从官网下载最新源码编译,根据提示对脚本做了修改,如下:

$ mencoder -ovc lavc -lavcopts vcodec=flv:vbitrate=500:mbd=2:last_pred=3:cmp=3:trell:v4mv:o=mpv_flags=+cbp_rd:o=mpv_flags=+qp_rd:mv0 -oac mp3lame -lameopts aq=3:preset=64 -lavdopts er=2 -pp 6 -of lavf -srate 44100 -ofps 12 -mc 0 ~/Videos/Wildlife.wmv -o ~/Videos/Wildlife.flv

转码依然有问题,求有经验的同学指点下编译时要注意的问题及脚本要做怎样的修改。

 

安装mplayer可选包

mplayer-doc - MPlayer documentation in various languages
mplayer-gui - GUI for MPlayer
mplayer-tools - Useful scripts for MPlayer

 

使用FFmpeg转码

从官网http://ffmpeg.org/download.html下载编译好的tar,解压后直接运行如下命令:

$ ./ffmpeg -y -i  ~/Videos/Wildlife.wmv -ar 44100 -vcodec libx264  ~/Videos/Wildlife.flv

播放转码后的视频比mencoder的更清晰,只是速度稍慢,ffmpeg使用更简单。

 

也可从github https://github.com/FFmpeg/FFmpeg下载源码编译,在INSTALL文件中有安装说明,注意末尾有:

 - Non system dependencies (e.g. libx264, libvpx) are disabled by default.

因此若要启用x264和lame,在运行./configure时要附加必要的参数,如:

 ./configure --enable-gpl --enable-libx264 --enable-libmp3lame

 

x264和lame下载地址 

x264 git clone git://git.videolan.org/x264.git (./configure --enable-shared)

lame http://sourceforge.net/projects/lame/

 

编译安装完毕后,执行ffmpeg命令时如出现如下错误:

ffmpeg: error while loading shared libraries: libx264.so.142: cannot open shared object file: No such file or directory

解决办法:

 # vi /etc/ld.so.conf
加入:/usr/local/lib

更改完毕执行:ldconfig

 

安装其他视频播放器测试

# yum install smplayer

# yum install vlc

 

删除视频播放器

删除mplayer(注意有个*哦,这样一次把mplayer, mplayer-common, mplayer-fonts, mencoder都删除了)

# yum erase mplayer*

 

系统自带的Movie Player不好用,可以考虑删除:

# yum erase totem

 

附录转码参数说明(4.8版):

 

mencoder -ovc lavc -lavcopts vcodec=flv:vbitrate=500:mbd=2:last_pred=3:cmp=3:trell:v4mv:cbp:qprd:mv0 -oac mp3lame -lameopts aq=3:preset=64 -lavdopts er=2 -pp 6 -of lavf -srate 44100 -ofps 12 -mc 0 {0} -o {1}

 

−ovc <codec name>
Encode with the given video codec (no default set).

 

-lavcopts
vcodec=<value> Employ the specified codec (default: mpeg4).
flv
Sorenson H.263 used in Flash Video

vbitrate=<value> Specify bitrate (default: 800).
WARNING: 1kbit = 1000 bits
4−16000 (in kbit)
16001−24000000 (in bit)

mbd=<0−2> (also see *cmp, qpel)
Macroblock decision algorithm (high quality mode), encode each macro block in all modes and choose the best. This is slow but results in better quality and file size. When mbd is set to 1 or 2, the value of mbcmp is ignored when comparing macroblocks (the mbcmp value is still used in other places though, in particular the motion search algorithms). If any comparison setting (precmp, subcmp, cmp, or mbcmp) is nonzero, however, a slower but better half-pel motion search will be used, regardless of what mbd is set to. If qpel is set, quarter-pel motion search will be used regardless.
0 Use comparison function given by mbcmp (default).
1 Select the MB mode which needs the fewest bits (=vhq).
2 Select the MB mode which has the best rate distortion.

last_pred=<0−99>
amount of motion predictors from the previous frame
0 (default)
a Will use 2a+1 x 2a+1 macroblock square of motion vector predictors from the previous frame.

cmp=<0−2000>
Sets the comparison function for full pel motion estimation (default: 0).

trell
Trellis searched quantization. This will find the optimal encoding for each 8x8 block. Trellis searched quantization is quite simply an optimal quantization in the PSNR versus bitrate sense (Assuming that there would be no rounding errors introduced by the IDCT, which is obviously not the case.). It simply finds a block for the minimum of error and lambda*bits.
lambda
quantization parameter (QP) dependent constant
bits
amount of bits needed to encode the block
error
sum of squared errors of the quantization

v4mv  
Allow 4 motion vectors per macroblock (slightly better quality). Works better if used with mbd>0.

cbp   
Rate distorted optimal coded block pattern. Will select the coded block pattern which minimizes distortion + lambda*rate. This can only be used together with trellis quantization.

qprd (mbd=2 only)
rate distorted optimal quantization parameter (QP) for the given lambda of each macroblock

mv0
Try to encode each MB with MV=<0,0> and choose the better one. This has no effect if mbd=0.

 

-lameopts
aq=<0−9> algorithmic quality (0 − best/slowest, 9 − worst/fastest)

preset=<value>
medium
VBR encoding, good quality, 150−180 kbps bitrate range
standard
VBR encoding, high quality, 170−210 kbps bitrate range
extreme
VBR encoding, very high quality, 200−240 kbps bitrate range
insane
CBR encoding, highest preset quality, 320 kbps bitrate
<8−320>
ABR encoding at average given kbps bitrate

 

−lavdopts <option1:option2:...> (DEBUG CODE)
Specify libavcodec decoding parameters. Separate multiple options with a colon.

er=<value>
Set error resilience strategy.
0: disabled
1: careful (Should work with broken encoders.)
2: normal (default) (Works with compliant encoders.)
3: aggressive (More checks, but might cause problems even for valid bitstreams.)
4: very aggressive

 

−pp <quality>
Set the DLL postprocess level. This option is no longer usable with −vf pp. It only works with Win32 DirectShow DLLs with internal postprocessing routines. The valid range of −pp values varies by codec, it is mostly 0−6, where 0=disable, 6=slowest/ best.

 

−of <format>
Encode to the specified container format (default: AVI).
EXAMPLE:
−of avi
Encode to AVI.
−of mpeg
Encode to MPEG (also see −mpegopts).
−of lavf
Encode with libavformat muxers (also see −lavfopts).
−of rawvideo
raw video stream (no muxing − one video stream only)
−of rawaudio
raw audio stream (no muxing − one audio stream only)

 

−srate <Hz>
Select the output sample rate to be used (of course sound cards have limits on this). If the sample frequency selected is different from that of the current media, the resample or lavcresample audio filter will be inserted into the audio filter layer to compensate for the difference. The type of resampling can be controlled by the −af−adv option. The default is fast resampling that may cause distortion.

 

−ofps <fps>
Specify a frames per second (fps) value for the output file, which can be different from that of the source material. Must be set for variable fps (ASF, some MOV) and progressive (30000/1001 fps telecined MPEG) files.

 

−mc <seconds/frame>
maximum A-V sync correction per frame (in seconds)
−mc 0 should always be combined with −noskip for mencoder, otherwise it will almost certainly cause A-V desync.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics