java调用视频转换工具ffmpeg
将flv文件转为mp4文件
1 2 3 |
Java代码 ffmpeg -i chendong.flv -c:v libx264 -crf 19 -strict experimental chendong.mp4 ffmpeg -i /mnt/D/work_documents/工作文档/公司文档/5.技术文档/zhendoc/vdo/2014suzhou/chendong.flv -c:v libx264 -crf 19 -strict experimental /mnt/D/work_documents/工作文档/公司文档/5.技术文档/zhendoc/vdo/2014suzhou/chendong.mp4 |
直接将文件转为flv文件
http://fanfq.iteye.com/blog/655569
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
Java代码 import java.io.File; import java.util.ArrayList; import java.util.Calendar; import java.util.List; public class ConvertVideo { private final static String PATH = "c:\\ffmpeg\\input\\c.mp4"; public static void main(String[] args) { if (!checkfile(PATH)) { System.out.println(PATH + " is not file"); return; } if (process()) { System.out.println("ok"); } } private static boolean process() { int type = checkContentType(); boolean status = false; if (type == 0) { System.out.println("直接将文件转为flv文件"); status = processFLV(PATH);// 直接将文件转为flv文件 } else if (type == 1) { String avifilepath = processAVI(type); if (avifilepath == null) return false;// avi文件没有得到 status = processFLV(avifilepath);// 将avi转为flv } return status; } private static int checkContentType() { String type = PATH.substring(PATH.lastIndexOf(".") + 1, PATH.length()) .toLowerCase(); // 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; } // 对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无法解析的文件格式(wmv9,rm,rmvb等), 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式. private static String processAVI(int type) { List<String> commend = new ArrayList<String>(); commend.add("c:\\ffmpeg\\mencoder"); commend.add(PATH); commend.add("-oac"); commend.add("lavc"); commend.add("-lavcopts"); commend.add("acodec=mp3:abitrate=64"); commend.add("-ovc"); commend.add("xvid"); commend.add("-xvidencopts"); commend.add("bitrate=600"); commend.add("-of"); commend.add("avi"); commend.add("-o"); commend.add("c:\\ffmpeg\\output\\a.avi"); try { ProcessBuilder builder = new ProcessBuilder(); builder.command(commend); builder.start(); return "c:\\ffmpeg\\output\\a.avi"; } catch (Exception e) { e.printStackTrace(); return null; } } // ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等) private static boolean processFLV(String oldfilepath) { if (!checkfile(PATH)) { System.out.println(oldfilepath + " is not file"); return false; } // 文件命名 Calendar c = Calendar.getInstance(); String savename = String.valueOf(c.getTimeInMillis())+ Math.round(Math.random() * 100000); List<String> commend = new ArrayList<String>(); commend.add("c:\\ffmpeg\\ffmpeg"); commend.add("-i"); commend.add(oldfilepath); commend.add("-ab"); commend.add("56"); commend.add("-ar"); commend.add("22050"); commend.add("-qscale"); commend.add("8"); commend.add("-r"); commend.add("15"); commend.add("-s"); commend.add("600x500"); commend.add("c:\\ffmpeg\\output\\a.flv"); try { Runtime runtime = Runtime.getRuntime(); Process proce = null; String cmd = ""; String cut = " c:\\ffmpeg\\ffmpeg.exe -i " + oldfilepath + " -y -f image2 -ss 8 -t 0.001 -s 600x500 c:\\ffmpeg\\output\\" + "a.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; } } } |