Android中使用FFmpeg

作者: AS_Vincent | 来源:发表于2017-10-31 14:45 被阅读0次

    在阅读这篇文章之前如果你的项目还未配置好FFmpeg的话,请先阅读AndroidStudio中配置FFmpeg

    1. 单独提取音频
    public static String[] extractAudio(String videoUrl, String outUrl) {
            String[] commands = new String[8];
            commands[0] = "ffmpeg";
            commands[1] = "-i";
            commands[2] = videoUrl;
            commands[3] = "-acodec";
            commands[4] = "copy";
            commands[5] = "-vn";
            commands[6] = "-y";
            commands[7] = outUrl;
            return commands;
        }
    videoUrl参数为视频的路径
    outUrl参数为存储路径
    
    1. 单独提取视频。没有声音
    public static String[] extractVideo(String videoUrl, String outUrl) {
            String[] commands = new String[8];
            commands[0] = "ffmpeg";
            commands[1] = "-i";
            commands[2] = videoUrl;
            commands[3] = "-vcodec";
            commands[4] = "copy";
            commands[5] = "-an";
            commands[6] = "-y";
            commands[7] = outUrl;
            return commands;
        }
    videoUrl参数为视频的路径
    outUrl参数为存储路径
    
    1. 裁剪音频
     public static String[] cutIntoMusic(String musicUrl, long second, String outUrl) {
            String[] commands = new String[10];
            commands[0] = "ffmpeg";
            commands[1] = "-i";
            commands[2] = musicUrl;
            commands[3] = "-ss";
            commands[4] = "00:00:10";
            commands[5] = "-t";
            commands[6] = String.valueOf(second);
            commands[7] = "-acodec";
            commands[8] = "copy";
            commands[9] = outUrl;
            return commands;
        }
    musicUrl参数为音频的路径
    outUrl参数为裁剪后的音频路径
    
    1. 原声和背景音乐的合成
      public static String[] composeAudio(String audio1, String audio2, String outputUrl) {
            String[] commands = new String[10];
            commands[0] = "ffmpeg";
            //输入
            commands[1] = "-i";
            commands[2] = audio1;
            //音乐
            commands[3] = "-i";
            commands[4] = audio2;
            //覆盖输出
            commands[5] = "-filter_complex";
            commands[6] = "amix=inputs=2:duration=first:dropout_transition=2";
            commands[7] = "-strict";
            commands[8] = "-2";
            //输出文件
            commands[9] = outputUrl;
            return commands;
        }
    audio1参数为原声的路径
    audio2参数为背景音乐的路径
    outputUrl参数为合成后的路径
    
    1. 修改音频文件的音量
       public static String[] changeAudioOrMusicVol(String audioOrMusicUrl, int vol, String outUrl) 
            String[] commands = new String[8];
            commands[0] = "ffmpeg";
            commands[1] = "-i";
            commands[2] = audioOrMusicUrl;
            commands[3] = "-vol";
            commands[4] = String.valueOf(vol);
            commands[5] = "-acodec";
            commands[6] = "copy";
            commands[7] = outUrl;
            return commands;
        }
    audioOrMusicUrl参数为音频路径
    vol参数为音频的音量
    outUrl参数为修改后音频的路径
    
    1. 音频和视频的合成
    public static String[] composeVideo(String videoUrl, String musicOrAudio, String outputUrl, long second) {
            String[] commands = new String[14];
            commands[0] = "ffmpeg";
            //输入
            commands[1] = "-i";
            commands[2] = videoUrl;
            //音乐
            commands[3] = "-i";
            commands[4] = musicOrAudio;
            commands[5] = "-ss";
            commands[6] = "00:00:00";
            commands[7] = "-t";
            commands[8] = String.valueOf(second);
            //覆盖输出
            commands[9] = "-vcodec";
            commands[10] = "copy";
            commands[11] = "-acodec";
            commands[12] = "copy";
            //输出文件
            commands[13] = outputUrl;
            return commands;
        }
    videoUrl参数为视频路径
    musicOrAudio参数为音频路径
    outputUrl参数为合成后的路径
    second参数为时间
    

    非常感谢大家的阅读,这些代码基本就可以满足简单的视频和音频的处理,有些人会有疑问该如何使用上面编写的这些代码呢?不着急,后续会出如何使用的文章,请需要的人关注留意。

    相关文章

      网友评论

        本文标题:Android中使用FFmpeg

        本文链接:https://www.haomeiwen.com/subject/chdbpxtx.html