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

1、首先下载ffmpeg(附件即是)
2、将解压得到的ffmpeg.exe和pthreadGC2.dll文件解压到任何目录(当然也可以是WebRoot中的目录)
3、创建bat文件convertVideo.bat并添加以下内容:( 参数说明见附录一)
Txt代码 复制代码
  1. %1\ffmpeg -i %2 -y -ab 56 -ar 22050 -b 500 -r 15 -s 320*240 %3  
  2. 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代码 复制代码
  1. 【转换文件格式的同时抓缩微图】   
  2. ffmpeg -i "test.avi" -y -f image2 -ss 8 -t 0.001 -s 350x240 "test.jpg"  
  3.   
  4. 【对已有flv抓图】   
  5. ffmpeg -i "test.flv" -y -f image2 -ss 8 -t 0.001 -s 350x240 "test.jpg"  
  6.   
  7. ****************   
  8. -ss后跟的时间单位为秒   
  9. ****************  
【转换文件格式的同时抓缩微图】
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代码 复制代码
  1. public class ConvertVideo   
  2. {   
  3.     private static String INPUT_PATH;   
  4.     private static String OUTPUT_PATH;   
  5.     private static String PROJECT_PATH;   
  6.     private static HashMap<String, String> fileType;   
  7.        
  8.     static  
  9.     {   
  10.         fileType = new HashMap<String, String>();   
  11.         fileType.put("avi""true");   
  12.         fileType.put("mpg""true");   
  13.         fileType.put("wmv""true");   
  14.         fileType.put("3gp""true");   
  15.         fileType.put("mov""true");   
  16.         fileType.put("mp4""true");   
  17.         fileType.put("asf""true");   
  18.         fileType.put("asx""true");   
  19.         fileType.put("flv""true");   
  20.     }   
  21.        
  22.     public static void convertToFLV(String projectPath, String inputFile, String outputFile)   
  23.     {   
  24.         INPUT_PATH = inputFile;   
  25.         OUTPUT_PATH = outputFile;   
  26.         PROJECT_PATH = projectPath;   
  27.         if (checkContentType())   
  28.             processFLV();// 直接将文件转为flv文件   
  29.     }   
  30.   
  31.     private static boolean checkContentType()   
  32.     {   
  33.         String type = INPUT_PATH.substring(INPUT_PATH.lastIndexOf(".") + 1, INPUT_PATH.length()).toLowerCase();   
  34.         // ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)   
  35.         return "true".equals(fileType.get(type));   
  36.     }   
  37.   
  38.     private static void processFLV()   
  39.     {   
  40.         if (new File(INPUT_PATH).isFile())   
  41.         {   
  42.             try  
  43.             {   
  44.                 String cmd = "cmd /c start X:\\ffmpeg.bat \"" + PROJECT_PATH + "\" \"" + INPUT_PATH + "\" \"" + OUTPUT_PATH +"\"";   
  45.                 Runtime.getRuntime().exec(cmd);   
  46.             }   
  47.             catch (Exception e)   
  48.             {   
  49.                 e.printStackTrace();   
  50.             }   
  51.         }   
  52.     }   
  53. }  
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代码 复制代码
  1. public static void main(String[] args)   
  2. {   
  3.     ConvertVideo.convertToFLV("X:\\convertVideo""X:\\a.wmv""X:\\a.flv");   
  4. }  
public static void main(String[] args)
{
	ConvertVideo.convertToFLV("X:\\convertVideo", "X:\\a.wmv", "X:\\a.flv");
}
 
6、附一个下载来的flv播放器吧(附件中下载)
 
Js代码 复制代码
  1. var swf_width="100%";   
  2. var swf_height="100%";   
  3. var files='nba.flv';   
  4. var config='0:自动播放|1:连续播放|100:默认音量|0:控制栏位置|2:控制栏显示|0x000033:主体颜色|60:主体透明度|0x66ff00:光晕颜色|0xffffff:图标颜色|0xffffff:文字颜色|:logo文字|:logo地址|:结束swf地址';   
  5. var texts='NBA精彩进球TOP10';   
  6. 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+'">');   
  7. document.write('<param name="movie" value="vcastr2.swf"/>');   
  8. document.write('<param name="quality" value="high"/>');   
  9. document.write('<param name="menu" value="false"/>');   
  10. document.write('<param name=wmode value="opaque"/>');   
  11. document.write('<param name="FlashVars" value="vcastr_file='+files+'&vcastr_title='+texts+'&vcastr_config='+config+'">');   
  12. 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" />');   
  13. 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
 
 

你可能感兴趣的:(职场,ffmpeg,休闲)