ffmpg簡介以及用它實現音頻視頻合並(java)


1.簡介
    FF
mpeg是一個自由軟件,可以運行音頻和視頻多種格式的錄影、轉檔、流功能。

 
2.下載
    源代碼 git://git.libav.org/libav.git
    Windows編譯版
http://ffmpeg.zeranoe.com/builds/
    下載最新版(lastest),可以下載static的build。用到壓縮包里的bin文件夾里的ffmpeg.exe。

 
3.ffmpeg基本命令:
    FFmpeg命令很多,如果你是在windows下開發的話,打開命令行,先進入你下載的FFmpeg.exe目錄(cd……),輸入ffmpeg -h即可查看各種命令。
下面就說說我們會用到的命令

格式轉換 (將file.avi 轉換成output.flv)
ffmpeg -i  file.avi   output.flv

現在有個視頻video.avi,有個音頻 audio.mp3,將其合並成output.avi
兩個命令                     ( video2.avi 是中間文件 ,用完可刪)
ffmpeg -i video.avi -vcodec copy -an video2.avi  
ffmpeg -i video2.avi -i audio.mp3 -vcodec copy -acodec copy output.avi
 -i 表示輸入文件
  -vcodec copy 表示 force video codec ('copy' to copy stream) 這個不知怎么譯 ,估計是直接copy
  -acodec copy   這個說的應該是音頻了   跟上面一樣
-an : 表示  disable audio  估計是audio no 之類的縮寫   表示去掉video.avi 原有的音頻

方法2 好像可以直接指定兩個輸入文件 ,
ffmpeg -i /tmp/a.wav -i /tmp/a.avi /tmp/a.avi 兩個文件 的順序很重
 
 
4.ffmpeg和java的結合
轉自:
http://blog.csdn.net/jimzhai/article/details/7853005
 
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; 
        }  
    }  

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM