您的位置:首页 > 其它

使用ffmpeg进行视频文件转换成FLV整理

2009-03-23 21:28 841 查看
1、首先下载ffmpeg(附件即是)
2、将解压得到的ffmpeg.exe和pthreadGC2.dll文件解压到任何目录(当然也可以是WebRoot中的目录)
3、创建bat文件convertVideo.bat并添加以下内容:(参数说明见附录一)

Txt代码



%1\ffmpeg -i %2 -y -ab 56 -ar 22050 -b 500 -r 15 -s 320*240 %3

exit
%1\ffmpeg -i %2 -y -ab 56 -ar 22050 -b 500 -r 15 -s 320*240 %3
exit

******************
%1为ffmpeg存放目录
%2为需要转换的文件路径
%3为FLV文件输出目录
******************
同样需要注意的是bat文件存放的路径不能包含空格
也可以对视频进行截取缩略图哦:)

Txt代码



【转换文件格式的同时抓缩微图】

ffmpeg -i "test.avi" -y -f image2 -ss 8 -t 0.001 -s 350x240 "test.jpg"

【对已有flv抓图】

ffmpeg -i "test.flv" -y -f image2 -ss 8 -t 0.001 -s 350x240 "test.jpg"

****************

-ss后跟的时间单位为秒

****************
【转换文件格式的同时抓缩微图】
ffmpeg -i "test.avi" -y -f image2 -ss 8 -t 0.001 -s 350x240 "test.jpg"
【对已有flv抓图】
ffmpeg -i "test.flv" -y -f image2 -ss 8 -t 0.001 -s 350x240 "test.jpg"
****************
-ss后跟的时间单位为秒
****************


4、创建类

Java代码



public class ConvertVideo

{

private static String INPUT_PATH;

private static String OUTPUT_PATH;

private static String PROJECT_PATH;

private static HashMap<String, String> fileType;

static

{

fileType = new HashMap<String, String>();

fileType.put("avi", "true");

fileType.put("mpg", "true");

fileType.put("wmv", "true");

fileType.put("3gp", "true");

fileType.put("mov", "true");

fileType.put("mp4", "true");

fileType.put("asf", "true");

fileType.put("asx", "true");

fileType.put("flv", "true");

}

public static void convertToFLV(String projectPath, String inputFile, String outputFile)

{

INPUT_PATH = inputFile;

OUTPUT_PATH = outputFile;

PROJECT_PATH = projectPath;

if (checkContentType())

processFLV();// 直接将文件转为flv文件

}

private static boolean checkContentType()

{

String type = INPUT_PATH.substring(INPUT_PATH.lastIndexOf(".") + 1, INPUT_PATH.length()).toLowerCase();

// ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)

return "true".equals(fileType.get(type));

}

private static void processFLV()

{

if (new File(INPUT_PATH).isFile())

{

try

{

String cmd = "cmd /c start X:\\ffmpeg.bat \"" + PROJECT_PATH + "\" \"" + INPUT_PATH + "\" \"" + OUTPUT_PATH +"\"";

Runtime.getRuntime().exec(cmd);

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

}
public class ConvertVideo
{
private static String INPUT_PATH;
private static String OUTPUT_PATH;
private static String PROJECT_PATH;
private static HashMap<String, String> fileType;

static
{
fileType = new HashMap<String, String>();
fileType.put("avi", "true");
fileType.put("mpg", "true");
fileType.put("wmv", "true");
fileType.put("3gp", "true");
fileType.put("mov", "true");
fileType.put("mp4", "true");
fileType.put("asf", "true");
fileType.put("asx", "true");
fileType.put("flv", "true");
}

public static void convertToFLV(String projectPath, String inputFile, String outputFile)
{
INPUT_PATH = inputFile;
OUTPUT_PATH = outputFile;
PROJECT_PATH = projectPath;
if (checkContentType())
processFLV();// 直接将文件转为flv文件
}
private static boolean checkContentType()
{
String type = INPUT_PATH.substring(INPUT_PATH.lastIndexOf(".") + 1, INPUT_PATH.length()).toLowerCase();
// ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
return "true".equals(fileType.get(type));
}
private static void processFLV()
{
if (new File(INPUT_PATH).isFile())
{
try
{
String cmd = "cmd /c start X:\\ffmpeg.bat \"" + PROJECT_PATH + "\" \"" + INPUT_PATH + "\" \"" + OUTPUT_PATH +"\"";
Runtime.getRuntime().exec(cmd);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}


5、测试代码

Java代码



public static void main(String[] args)

{

ConvertVideo.convertToFLV("X:\\convertVideo", "X:\\a.wmv", "X:\\a.flv");

}
public static void main(String[] args)
{
ConvertVideo.convertToFLV("X:\\convertVideo", "X:\\a.wmv", "X:\\a.flv");
}


6、附一个下载来的flv播放器吧(附件中下载)

Js代码



var swf_width="100%";

var swf_height="100%";

var files='nba.flv';

var config='0:自动播放|1:连续播放|100:默认音量|0:控制栏位置|2:控制栏显示|0x000033:主体颜色|60:主体透明度|0x66ff00:光晕颜色|0xffffff:图标颜色|0xffffff:文字颜色|:logo文字|:logo地址|:结束swf地址';

var texts='NBA精彩进球TOP10';

document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="'+swf_width+'" height="'+swf_height+'">');

document.write('<param name="movie" value="vcastr2.swf"/>');

document.write('<param name="quality" value="high"/>');

document.write('<param name="menu" value="false"/>');

document.write('<param name=wmode value="opaque"/>');

document.write('<param name="FlashVars" value="vcastr_file='+files+'&vcastr_title='+texts+'&vcastr_config='+config+'">');

document.write('<embed src="vcastr2.swf"/>" wmode="opaque" FlashVars="vcastr_file='+files+'&vcastr_title='+texts+'&vcastr_config='+config+'" menu="false" quality="high" width="'+swf_width+'" height="'+swf_height+'" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');

document.write('</object>');
var swf_width="100%";
var swf_height="100%";
var files='nba.flv';
var config='0:自动播放|1:连续播放|100:默认音量|0:控制栏位置|2:控制栏显示|0x000033:主体颜色|60:主体透明度|0x66ff00:光晕颜色|0xffffff:图标颜色|0xffffff:文字颜色|:logo文字|:logo地址|:结束swf地址';
var texts='NBA精彩进球TOP10';
document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="'+swf_width+'" height="'+swf_height+'">');
document.write('<param name="movie" value="vcastr2.swf"/>');
document.write('<param name="quality" value="high"/>');
document.write('<param name="menu" value="false"/>');
document.write('<param name=wmode value="opaque"/>');
document.write('<param name="FlashVars" value="vcastr_file='+files+'&vcastr_title='+texts+'&vcastr_config='+config+'">');
document.write('<embed src="vcastr2.swf"/>" wmode="opaque" FlashVars="vcastr_file='+files+'&vcastr_title='+texts+'&vcastr_config='+config+'" menu="false" quality="high" width="'+swf_width+'" height="'+swf_height+'" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
document.write('</object>');


【补充说明】如果遇到ffmpeg所涉及以外的视频格式,可以使用其他转换工具先转换,例如mencoder。

附录一(ffmpeg参数说明):

【参数】【说明】【示例】
-i "路径"指定需要转换的文件路径-i "C:\nba.wmv"
-y覆盖输出文件,即如果nba.xxx文件已经存在的话,直接覆盖
控制画面参数
-bitexac使用标准比特率
-vcodec xvid使用xvid压缩
-s <宽高比>指定分辨率大小-s 320*240
-r <数值>帧速率(非标准数值会导致音画不同步【标准值为15或29.97】)-r 15
-b <比特率>指定压缩比特率-b 1500
-qmin <数值>设定最小质量-qmin 10
-qmax <数值>与-qmin相反,可以与-qmin同时使用-qmax 30
-sameq使用与源视频相同的质量
控制声音参数
-acodec aac设定声音编码
-ac <数值>设定声道数:1为单声道,2为立体声-ac 2
-ar <采样率>设定声音采样率(8000,11025,22050)-ar 22050
-ab <比特率>设定声音比特率(-ac设为立体声时要以一半比特率来设置,比如192kbps的就设成96)-ab 96
-vol <百分比>设定音量大小,比如设定200就会比原来的音量大2倍-vol 200
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: