用mac版的ffmpeg可以直接支持h264编码,
用ubuntu的版本默认是不带h264压缩编码,只支持解码。
要像制作h264的文件,需要编译相关的源码。
参考的文章:
http://stackoverflow.com/questions/5678695/ffmpeg-usage-to-encode-a-video-to-h264-codec-format
I believe that by now the above answers are outdated (or at least unclear) so here's my little go at it. I tried compiling ffmpeg with the option --enable-encoders=libx264
and it will give no error but it won't enable anything (I can't seem to find where I found that suggestion).
Anyways step-by-step, first you must compile libx264 yourself because repository version is outdated:
wget ftp://ftp.videolan.org/pub/x264/snapshots/last_x264.tar.bz2
tar --bzip2 -xvf last_x264.tar.bz2
cd x264-snapshot-XXXXXXXX-XXXX/
sudo ./configure
sudo make
sudo make install
And then get and compile ffmpeg with libx264 enabled. I'm using the latest release which is "Happiness":
wget http://ffmpeg.org/releases/ffmpeg-0.11.2.tar.bz2
tar --bzip2 -xvf ffmpeg-0.11.2.tar.bz2
cd ffmpeg-0.11.2/
sudo ./configure --enable-libx264 --enable-gpl
sudo make
sudo install
Now finally you have the libx264 codec to encode, to check it you may run
ffmpeg -codecs | grep h264
and you'll see the options you have were the first D means decoding and the first E means encoding
然后即使这样做,还是会在configure ffmpeg时出现找不到x264 lib的问题,然后幸好有人补充说要安装x264的静态库。
I found that make install for libx264 installed only the executable x264 program, but not the libraries. After I used install-lib-dev install-lib-shared and install-lib-static it worked fine (I'm not sure which of these is necessary but I succeeded with all three). –
Leigh Caldwell
Jan 3 '13 at 21:38
|
他说不确定是安装共享库还是静态库,我可以告诉他是最后一个,因为我逐一尝试,只有装完最后一个才ok的。
使用的方法:
ffmpeg的参数非常多,但我们实际上不需要全面了解,我们是要他干活的,不是拿他来研究的。
ffmpeg -h
显示帮助信息
ffmpeg -i Downloads/宙斯之子2014-1080.mp4 -s 1916x796 -b:v 600k herculet.mp4
把一个mp4文件压缩成码率是600kbps的mp4文件,由于没有指定编码格式,系统会用mpeg4v2来编码,这不是我想要的,我希望用h264,但遇到了一点问题,就是系统没有默认安装。 ffmpeg -i /home/huziqin/hercule2014-1080.mp4 -vcodec libx264 -s 1916x796 -vb 3000k -acodec copy /home/share/her-merge2.mp4
这条命令才是压制成h264格式的,但是压制成h264格式占有的CPU资源很大,12线程的CPU没秒钟才完成10帧左右。
======================================
压出来的视频在电脑上能播放,但在安卓上不能播放,
估计是需要加一些参数,例如:profile,preset,bufsize之类
用这条命令压缩前面2分钟然后停止,大概50M左右大小的文件,放进安卓机器是可以播放,问题是不知道压完之后还行不行,压完一个文件要一晚上,明天才知道。
ffmpeg -i /home/huziqin/hercule2014-1080.mp4 -codec:v libx264 -profile:v high -preset slow -b:v 3000k -maxrate 6000k -bufsize 1000k -vf scale=-1:796 -threads 0 -strict -2 -codec:a aac -b:a 128k /home/share/output_file3.mp4
这个问题经过分析播放器的log,发现播放器需要识别为matroska格式的容器才能打开,但如果压缩成mp4格式,则当文件比较小的时候能打开,但文件大了就打不开。
bool CDVDDemuxFFmpeg::Open(CDVDInputStream*, bool, bool) - probing detected format [mov,mp4,m4a,3gp,3g2,mj2]
V/XBMC ( 3275): Debug Print: bool CDVDDemuxFFmpeg::Open(CDVDInputStream*, bool, bool) - Error, could not open file /mnt/usbhost1/720P/her-merge-aac2.mp4
V/XBMC ( 3275): Debug Print: static bool CDVDFileInfo::ExtractThumb(const string&, CTextureDetails&, CStreamDetails*) - Error creating demuxer
bool CDVDDemuxFFmpeg::Open(CDVDInputStream*, bool, bool) - probing detected format [matroska,webm]
V/XBMC ( 3275): Debug Print: bool CDVDDemuxFFmpeg::Open(CDVDInputStream*, bool, bool) - avformat_find_stream_info starting
V/XBMC ( 3275): Debug Print: bool CDVDDemuxFFmpeg::Open(CDVDInputStream*, bool, bool) - av_find_stream_info finished
V/XBMC ( 3275): Debug Print: Openi
开始我不知道matroska是什么,后来查了才知道就是mkv格式。简单来说就是这个播放器只支持比较小的mp4,如果文件大的话超过2G,就要压成mkv才可以识别。
因此把原先压好的文件用ffmpeg重新封装一遍就可以了。
ffmpeg -i /home/share/output_file4.mp4 -c:v copy -c:a copy -f matroska /home/share/output_file5.mkv