公司才搬完屋,正在四出搜尋附近的食店之際,一群正在使用TVParty Suite的同事就來投訴FLV的畫質很糟糕。適逢上星期Adobe發佈了最新的Flash Player 9 update 3版本,加入了備受注目的H.264影片支援,米奇正好就借這個機會來替TVParty Suite高清化吧!
不幸的是,新版本Flash Player 9 所支援的H.264影片是使用HE-AAC,而ffmpeg所使用的libfaac又無法壓製這個新格式,所以就要借助外力幫助,將影像和聲音分開處理,使用由Nero提供的neroAacEnc編碼器來壓製音頻。大家只要到這裡就可以下載到這個免費的HE-AAC編碼器,解壓後,將linux目錄內的檔案neroAacEnc和neroAacDec上傳到伺服器/usr/local/bin目錄內,並設定權限為0755就能夠使用,連編譯的工夫也省了。
接下來,為了將ffmpeg壓製出來的純影像和neroAacEnc壓製出來的音訊合併成MP4檔,又要安裝一個叫GPAC的工具套件。GPAC提供很多編輯MP4和3GP檔案的工具,包括可以將視頻和音頻合成一個MP4/3GP檔,甚至還可以串流MP4檔案。這正好適合用來將使用neorAacEnc編碼的HE-AAC音頻與ffmpeg所製成的H.264影片合成起來。
GPAC下載網址:http://gpac.sourceforge.net/home_download.php
# tar zxvf gpac-0.4.4.tar.gz
# cd gpac
# ./configure && make && make install
雖然這一堆軟件的使用方法都非常簡單,不過工序就很煩複,單是聽聽就已經讓你覺得頭痛吧?幸好有高人指點,Creating good quality h264 video for the upcoming Flash Player and Flash Media Server這篇文章就介紹了一個shell script,可以將這一切程序封裝成一個指令,這樣用起來就方便多了。米奇將它加以修改,貼出來分享一下。
encode_h264.sh
#!/bin/bash
# H.264 encode script
# for Flash Player 9 Update 3 (9.0.115.0)
#
# Parameters:
# $1 absolute path to source file; ${1%.*} to remove the extension
# $2 target file path# Configuration
dimension="640x368"
video_bitrate=500
audio_bitrate=64
audio_channel=2begin_time=`date +%s`
# Video part
ffmpeg -y -i $1 -an -pass 1 -vcodec libx264 -b ${video_bitrate}k -s $dimension -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -flags2 +mixed_refs -me umh -subq 5 -trellis 1 -refs 3 -bf 3 -b_strategy 1 -coder 1 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt ${video_bitrate}k -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.8 -qmin 10 -qmax 51 -qdiff 4 ${1%.*}_temp_video.mp4ffmpeg -y -i $1 -an -pass 2 -vcodec libx264 -b ${video_bitrate}k -s $dimension -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -flags2 +mixed_refs -me umh -subq 5 -trellis 1 -refs 3 -bf 3 -b_strategy 1 -coder 1 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt ${video_bitrate}k -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.8 -qmin 10 -qmax 51 -qdiff 4 ${1%.*}_temp_video.mp4
# Audio Part
ffmpeg -y -i $1 -ar 48000 -ac 2 ${1%.*}_temp_audio.wav
neroAacEnc -br $((audio_bitrate*1000)) -he -if ${1%.*}_temp_audio.wav -of ${1%.*}_temp_audio.mp4# Remove old file
if [ -f $2 ]; then
rm -f $2
fi# Muxing everything
MP4Box -add ${1%.*}_temp_video.mp4#video $2
MP4Box -add ${1%.*}_temp_audio.mp4#audio $2
MP4Box -inter 500 $2# Metadata
MP4Box -lang Chinese $2# Swap temps
rm -f ${1%.*}_temp*now=`date +"%F %T"`
end_time=`date +%s`
echo "[""$now""] Encode Finish (Total time: " $(($end_time-$begin_time)) " sec)"
exit
將這個檔案存成UNIX文檔,上傳到/usr/local/lib/目錄,然後將這文檔的權限改為0755就可以使用。這個程式的使用方法很簡單,只要輸入encode_h264.sh <來源檔路徑> <目的檔路徑>便可以產生出質素很高的MP4影片。當中有些bitrate和影片尺寸的參數大家可以自行修改。
今後米奇就會以這個script來取代TVParty Suite原本的壓片程序,全面使用H.264。






