- //视频格式转换函数
- public static boolean process(String resourcePath)
- {
- int type = checkContentType(resourcePath); //先检测源文件类型是不是ffmpeg能解析的格式
- boolean status = false;
- if (type == 0)
- {
- status = processFLV(resourcePath);// 直接将文件转为flv文件
- }
- return status;
- }
- //判断参数的视频格式是不是
- private static int checkContentType(String resourcePath)
- {
- String type = resourcePath.substring(resourcePath.lastIndexOf(".") + 1,
- resourcePath.length()).toLowerCase();
- // cuplayer.com ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
- if (type.equals("avi"))
- {
- return 0;
- }
- else if (type.equals("mpg"))
- {
- return 0;
- }
- else if (type.equals("wmv"))
- {
- return 0;
- }
- else if (type.equals("3gp"))
- {
- return 0;
- }
- else if (type.equals("mov"))
- {
- return 0;
- }
- else if (type.equals("mp4"))
- {
- return 0;
- }
- else if (type.equals("asf"))
- {
- return 0;
- }
- else if (type.equals("asx"))
- {
- return 0;
- }
- else if (type.equals("flv"))
- {
- return 0;
- }
- else if (type.equals("mpeg"))
- {
- return 0;
- }
- else if (type.equals("mpe"))
- {
- return 0;
- }
- // cuplayer.com 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等), 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.
- else if (type.equals("wmv9"))
- {
- return 1;
- }
- else if (type.equals("rm"))
- {
- return 1;
- }
- else if (type.equals("rmvb"))
- {
- return 1;
- }
- return 9;
- }
- private static boolean checkfile(String path)
- {
- File file = new File(path);
- if (!file.isFile())
- {
- return false;
- }
- return true;
- }
- // ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
- private static boolean processFLV(String resourcePath)
- {
- if (!checkfile(resourcePath))
- {
- System.out.println(resourcePath + " is not file");
- return false;
- }
- // 文件命名
- Calendar c = Calendar.getInstance();
- String savename = String.valueOf(c.getTimeInMillis())+ Math.round(Math.random() * 100000);
- List commend = new ArrayList();
- commend.add("e:\\ffmpeg");
- commend.add("-i");
- commend.add(resourcePath);
- // commend.add("-ab"); //音频流码率:(默认是同源文件码率)
- // commend.add("56");
- commend.add("-ar"); //视频流采样率:(大多数情况下使用44100和48000,分别对用PAL和NTSC制式,根据需要选择)
- commend.add("22050");
- commend.add("-qscale"); //视频量化指标
- commend.add("8");
- commend.add("-r"); //视频流帧数(一般来书PAL制式同常用25,ntsc制式通常用29)
- commend.add("15");
- commend.add("-s"); //视频解析度:(分辨率)可以自己定义所需要的大小: 改变视频流的解析式很耗cpu的
- commend.add("600x500");
- commend.add("e:\\" + savename + ".flv");
- try {
- Runtime runtime = Runtime.getRuntime();
- Process proce = null;
- String cmd = "";
- String cut = " e://ffmpeg.exe -i "
- + resourcePath
- + " -y -f image2 -ss 8 -t 0.001 -s 600x500 e:\\"
- + savename + ".jpg";
- String cutCmd = cmd + cut;
- proce = runtime.exec(cutCmd);
- ProcessBuilder builder = new ProcessBuilder(commend);
- // builder.command(commend);
- builder.start();
- return true;
- }
- catch (Exception e)
- {
- e.printStackTrace();
- return false;
- }
- }
- public static void main(String[] args)
- {
- if (!checkfile("e:\\1-android_sdk.flv"))
- {
- System.out.println("" + " is not file");
- return;
- }
- if (process("e:\\1-android_sdk.flv"))
- {
- System.out.println("ok");
- }
- }
应用ffmpeg进行视频文件格式转换
最新推荐文章于 2024-08-13 04:08:27 发布