用fms2做流媒體伺服器,就需要把所有使用者上傳的各種視頻轉換成flv格式,這種格式的檔案容量很小,比較適合遠程播放。用ffmpeg和mencoder已經足以把絕大部分視頻轉換成flv了。
我這裡是用perl調用ffmpeg和mecoder命令,java操作資料庫和控制線程調用perl進行轉換的。說也說不清,我還是直接拿源碼來說吧:
1、covert.pl
#!/usr/bin/perl
my $encoder = $ARGV[0];//java傳過來的參數,編碼命令,如:/usr/local/ffmepg
my $fileIn = $ARGV[1];//java傳過來的參數,要轉換的源檔案路徑
my $fileOut = $ARGV[2];//java傳過來的參數,轉換後的檔案路徑
my $logPath = $ARGV[3];//java傳過來的參數,日誌路徑
my $localTime = localtime;//目前時間
my $cmd;
my $coder = substr($encoder,rindex($encoder,"/")+1);
if($coder eq "ffmpeg"){
$cmd = $encoder." -i ".$fileIn." -ab 64 -acodec mp3 -ac 1 -ar 22050 -b 230 -r 29.97 -y ".$fileOut;//如果轉換命令是ffmpeg,調用該命令轉換
}
else{//否則調用該命令用ffmpeg轉換
$cmd = $encoder." -of lavf -lavfopts
i_certify_that_my_video_stream_does_not_use_b_frames -ovc lavc
-lavcopts vcodec=flv:vbitrate=500 -srate 22050 -oac lavc -lavcopts
acodec=mp3:abitrate=56 -ffourcc FLV1 -oac mp3lame ".$fileIn." -o
".$fileOut;
}
`echo $localTime: $cmd >> $logPath`;//把命令內容寫到日誌
`$cmd`;//執行轉換命令
2、CovertVideo.java
ffmpeg轉換flv:
public synchronized static boolean ffmpegToFlv(String fileIn, String fileOut) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String cmdFile = Configuration.getInstance()//從設定檔擷取perl檔案路徑
.getConfigValue("path.perl")
+ "covert.pl";
String encoder = Configuration.getInstance().getConfigValue(//從設定檔擷取命令列路徑
"commend.ffmpeg");
String logPath = Configuration.getInstance().getConfigValue(//日誌路徑
"stdout.path")
+ format.format(new Date()) + ".txt";
StringBuffer cmd = new StringBuffer("/usr/bin/perl ").append(cmdFile)
.append(" ").append(encoder).append(" ").append(fileIn).append(
" ").append(fileOut).append(" ").append(logPath);
System.out.print(cmd.toString());
String line = null;
InputStream stderr = null;
InputStreamReader isr = null;
BufferedReader br = null;
Runtime rt = null;
boolean success = false;
try {
rt = Runtime.getRuntime();
Process proc = rt.exec(cmd.toString());//執行命令,調用perl進行轉換
stderr = proc.getErrorStream();
isr = new InputStreamReader(stderr);
br = new BufferedReader(isr);
System.out.println("<ffmpegToFlv>");
while ((line = br.readLine()) != null)
System.out.println(line);
System.out.println("</ffmpegToFlv>");
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
File filePath = new File(fileOut);
// 如果檔案存在,並且長度不為0,則表示轉換成功.
success = filePath.exists() && filePath.length() > 0;
} catch (Throwable t) {
t.printStackTrace();
} finally {
try {
stderr.close();
isr.close();
br.close();
} catch (Exception e) {
e.printStackTrace();
rt.exit(1);
}
}
return success;
}
mencoder轉換flv跟上面幾乎一樣,不寫注釋了:
public synchronized static boolean mencoderToFlv(String fileIn,
String fileOut) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String cmdFile = Configuration.getInstance()
.getConfigValue("path.perl")
+ "covert.pl";
String encoder = Configuration.getInstance().getConfigValue(
"commend.mencoder");
String logPath = Configuration.getInstance().getConfigValue(
"stdout.path")
+ format.format(new Date()) + ".txt";
StringBuffer cmd = new StringBuffer("/usr/bin/perl ").append(cmdFile)
.append(" ").append(encoder).append(" ").append(fileIn).append(
" ").append(fileOut).append(" ").append(logPath);
System.out.print(cmd.toString());
String line = null;
InputStream stderr = null;
InputStreamReader isr = null;
BufferedReader br = null;
Runtime rt = null;
boolean success = false;
try {
rt = Runtime.getRuntime();
Process proc = rt.exec(cmd.toString());
stderr = proc.getErrorStream();
isr = new InputStreamReader(stderr);
br = new BufferedReader(isr);
System.out.println("<mencoderToFlv>");
while ((line = br.readLine()) != null)
System.out.println(line);
System.out.println("</mencoderToFlv>");
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
File filePath = new File(fileOut);
// 如果檔案存在,並且長度不為0,則表示轉換成功.
success = filePath.exists() && filePath.length() > 0;
} catch (Throwable t) {
t.printStackTrace();
} finally {
try {
stderr.close();
isr.close();
br.close();
} catch (Exception e) {
e.printStackTrace();
rt.exit(1);
}
}
return success;
}
程式已經編譯通過的,設定檔config.properties需要放到web服務的WEB-INF/classes目錄下,只需按實際情況把設定檔裡面的路徑修改成你自己的就可以用了。