linux下使用ffmpeg将flv、mp4、rmvb转换为libx264的mp4

    xiaoxiao2021-03-25  129

    1.示例

           ffmpeg -i  testrmvb.rmvb  -c:v libx264 -strict -2 testrmvb.mp4

    2.java批量转换

      

    public static void dirGoThrough(){ File baseDir=new File("/home/wwwroot/www/shs/upload/file/");//遍历的文件夹 File[] fs = baseDir.listFiles(); int total = 0; int mp4total = 0; if(fs!=null){ for(File dir:fs){ File[] files = dir.listFiles(); for(File f:files){ if(f.isFile()){ if(f.getName().toLowerCase().endsWith(".flv")||f.getName().toLowerCase().endsWith(".mp4")|| f.getName().toLowerCase().endsWith(".rmvb")){ boolean flag = ffmpegTransfer(f.getAbsolutePath()); if(!flag){ LogWriteDao.WriteLog(f.getAbsolutePath(), "transfererror", "/pdf/log/"); } total++; } // if(f.getName().toLowerCase().endsWith(".mp4")){ // mp4total++; // } } } } } System.out.println("总数量:"+total+"--mp4的总数量:"+mp4total); LogWriteDao.WriteLog("total:"+total, "transferfinished", "/pdf/log/"); } public static boolean ffmpegTransfer(String infile) { String outfile = infile.replace(".mp4", "_libx264.mp4").replace(".flv", "_libx264.mp4") .replace(".rmvb", "_libx264.mp4"); String avitoflv = "ffmpeg -i "+infile+" -c:v libx264 -strict -2 "+outfile; try { Runtime rt = Runtime.getRuntime(); Process proc = rt.exec(new String[]{"sh","-c",avitoflv}); InputStream stderr = proc.getErrorStream(); InputStreamReader isr = new InputStreamReader(stderr); BufferedReader br = new BufferedReader(isr); String line = null; while ( (line = br.readLine()) != null) System.out.println(line); int exitVal = proc.waitFor(); System.out.println("Process exitValue: " + exitVal); } catch (Throwable t) { t.printStackTrace(); return false; } return true; }

           

    转载请注明原文地址: https://ju.6miu.com/read-10072.html

    最新回复(0)