简体   繁体   English

如何使用 ffmpeg 将新音频(不混合)添加到视频中?

[英]How to add a new audio (not mixing) into a video using ffmpeg?

I used a command like:我使用了如下命令:

ffmpeg -i video.avi -i audio.mp3 -vcodec codec -acodec codec output_video.avi -newaudio

in latest version for adding new audio track to video (not mix).在最新版本中为视频添加新的音轨(不是混音)。

But I updated the ffmpeg to the newest version ( ffmpeg version git-2012-06-16-809d71d ) and now in this version the parameter -newaudio doesn't work.但是我将 ffmpeg 更新到最新版本( ffmpeg 版本 git-2012-06-16-809d71d ),现在在这个版本中参数-newaudio不起作用。

Tell me please how I can add new audio to my video (not mix) using ffmpeg .请告诉我如何使用ffmpeg将新音频添加到我的视频(而不是混合)。

Replace audio替换音频

音频流替换示意图

ffmpeg -i video.mp4 -i audio.wav -map 0:v -map 1:a -c:v copy -shortest output.mp4
  • The -map option allows you to manually select streams / tracks. -map选项允许您手动选择流/轨道。 See FFmpeg Wiki: Map for more info.有关更多信息,请参阅FFmpeg Wiki:地图
  • This example uses -c:v copy to stream copy (mux) the video.此示例使用-c:v copy流式复制(复用)视频。 No re-encoding of the video occurs.不会发生视频的重新编码。 Quality is preserved and the process is fast.质量得到保证,过程快速。
    • If your input audio format is compatible with the output format then change -c:v copy to -c copy to stream copy both the video and audio.如果输入的音频格式与输出格式兼容然后更改-c:v copy-c copy到流拷贝这两个视频和音频。
    • If you want to re-encode video and audio then remove -c:v copy / -c copy .如果要重新编码视频和音频,请删除-c:v copy / -c copy
  • The -shortest option will make the output the same duration as the shortest input. -shortest选项将使输出的持续时间与最短的输入相同。

Add audio添加音频

音频流添加示意图

ffmpeg -i video.mkv -i audio.mp3 -map 0 -map 1:a -c:v copy -shortest output.mkv
  • The -map option allows you to manually select streams / tracks. -map选项允许您手动选择流/轨道。 See FFmpeg Wiki: Map for more info.有关更多信息,请参阅FFmpeg Wiki:地图
  • This example uses -c:v copy to stream copy (mux) the video.此示例使用-c:v copy流式复制(复用)视频。 No re-encoding of the video occurs.不会发生视频的重新编码。 Quality is preserved and the process is fast.质量得到保证,过程快速。
    • If your input audio format is compatible with the output format then change -c:v copy to -c copy to stream copy both the video and audio.如果输入的音频格式与输出格式兼容然后更改-c:v copy-c copy到流拷贝这两个视频和音频。
    • If you want to re-encode video and audio then remove -c:v copy / -c copy .如果要重新编码视频和音频,请删除-c:v copy / -c copy
  • The -shortest option will make the output the same duration as the shortest input. -shortest选项将使输出的持续时间与最短的输入相同。

Mixing/combining two audio inputs into one将两个音频输入混合/组合为一个

音频缩混示意图

Use video from video.mkv .使用video.mkv Mix audio from video.mkv and audio.m4a using the amerge filter :从混合音频video.mkvaudio.m4a使用amerge过滤器

ffmpeg -i video.mkv -i audio.m4a -filter_complex "[0:a][1:a]amerge=inputs=2[a]" -map 0:v -map "[a]" -c:v copy -ac 2 -shortest output.mkv

See FFmpeg Wiki: Audio Channels for more info.有关更多信息,请参阅FFmpeg Wiki:音频频道

Generate silent audio生成静音音频

You can use the anullsrc filter to make a silent audio stream.您可以使用anullsrc 过滤器制作无声音频流。 The filter allows you to choose the desired channel layout (mono, stereo, 5.1, etc) and the sample rate.过滤器允许您选择所需的通道布局(单声道、立体声、5.1 等)和采样率。

ffmpeg -i video.mp4 -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 \
-c:v copy -shortest output.mp4

Also see另见

mp3 music to wav mp3 音乐到 wav

ffmpeg -i music.mp3 music.wav

truncate to fit video截断以适合视频

ffmpeg -i music.wav -ss 0 -t 37 musicshort.wav

mix music and video混合音乐和视频

ffmpeg -i musicshort.wav -i movie.avi final_video.avi

If the input video has multiple audio tracks and you need to add one more then use the following command:如果输入视频有多个音轨并且您需要再添加一个,则使用以下命令:

ffmpeg -i input_video_with_audio.avi -i new_audio.ac3 -map 0 -map 1 -codec copy output_video.avi

-map 0 means to copy (include) all streams from the first input file ( input_video_with_audio.avi ) and -map 1 means to include all streams (in this case one) from the second input file ( new_audio.ac3 ). -map 0表示从第一个输入文件( input_video_with_audio.avi )复制(包括)所有流, -map 1表示从第二个输入文件( new_audio.ac3 )包括所有流(在本例中为一个)。

None of these solutions quite worked for me.这些解决方案都不适合我。 My original audio was being overwritten, or I was getting an error like "failed to map memory" with the more complex 'amerge' example.我的原始音频被覆盖,或者我在更复杂的“amerge”示例中收到“无法映射内存”之类的错误。 It seems I needed -filter_complex amix.看来我需要 -filter_complex 混合。

ffmpeg -i videowithaudioyouwanttokeep.mp4 -i audiotooverlay.mp3 -vcodec copy -filter_complex amix -map 0:v -map 0:a -map 1:a -shortest -b:a 144k out.mkv

没有什么对我有用(我认为这是因为我输入的 .mp4 视频没有任何音频)所以我发现这对我有用:

ffmpeg -i input_video.mp4 -i balipraiavid.wav -map 0:v:0 -map 1:a:0 output.mp4

If you are using an old version of FFMPEG and you cant upgrade you can do the following:如果您使用的是旧版本的 FFMPEG 并且无法升级,您可以执行以下操作:

ffmpeg -i PATH/VIDEO_FILE_NAME.mp4 -i PATH/AUDIO_FILE_NAME.mp3 -vcodec copy -shortest DESTINATION_PATH/NEW_VIDEO_FILE_NAME.mp4

Notice that I used -vcodec请注意,我使用了-vcodec

Code to add audio to video using ffmpeg.使用 ffmpeg 将音频添加到视频的代码。

If audio length is greater than video length it will cut the audio to video length.如果音频长度大于视频长度,它会将音频剪切为视频长度。 If you want full audio in video remove -shortest from the cmd.如果您想要视频中的完整音频,请从 cmd 中删除 -shortest。

String[] cmd = new String[]{"-i", selectedVideoPath,"-i",audiopath,"-map","1:a","-map","0:v","-codec","copy", ,outputFile.getPath()};

private void execFFmpegBinaryShortest(final String[] command) {



            final File outputFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/videoaudiomerger/"+"Vid"+"output"+i1+".mp4");




            String[] cmd = new String[]{"-i", selectedVideoPath,"-i",audiopath,"-map","1:a","-map","0:v","-codec","copy","-shortest",outputFile.getPath()};


            try {

                ffmpeg.execute(cmd, new ExecuteBinaryResponseHandler() {
                    @Override
                    public void onFailure(String s) {
                        System.out.println("on failure----"+s);
                    }

                    @Override
                    public void onSuccess(String s) {
                        System.out.println("on success-----"+s);
                    }

                    @Override
                    public void onProgress(String s) {
                        //Log.d(TAG, "Started command : ffmpeg "+command);
                        System.out.println("Started---"+s);

                    }

                    @Override
                    public void onStart() {


                        //Log.d(TAG, "Started command : ffmpeg " + command);
                        System.out.println("Start----");

                    }

                    @Override
                    public void onFinish() {
                        System.out.println("Finish-----");


                    }
                });
            } catch (FFmpegCommandAlreadyRunningException e) {
                // do nothing for now
                System.out.println("exceptio :::"+e.getMessage());
            }


        }

How to merge all audio tracks into one an entire directory with ffmpeg?如何使用 ffmpeg 将所有音轨合并到一个完整的目录中? ffmpeg -i video.mp4 -i audio.mp3 -map 0:v -map 1:a -c:v copy -shortest output.mp4 ffmpeg -i video.mp4 -i audio.mp3 -map 0:v -map 1:a -c:v copy -shortest output.mp4

The marked answer does not set the audio track's language.标记的答案不会设置音轨的语言。

The following is an example that specifies German for the default audio track (the video's only audio channel) and English for the audio track that is added anew:以下示例为默认音轨(视频的唯一音频通道)指定德语,为新添加的音轨指定英语:

ffmpeg -i "/dir/video.mkv" -i "/dir2/audio.ac3" -map 0 -map 1:a -c:v copy -shortest -metadata:s:a:0 language=ger -metadata:s:a:0 title="GER" -metadata:s:a:1 language=eng -metadata:s:a:1 title="ENG" "/dir/output.mkv"

(The s:a:0 starts counting from 0, adjust that number as needed. If the audio track language is already specified, you don't need to set it and would only need to set it for the audio track that you add.) s:a:0从0开始计数,根据需要调整该数字。如果已经指定了音轨语言,则不需要设置它,只需要为您添加的音轨设置它。 )

Here is how I did what the OP wanted.这是我如何做 OP 想要的。

My setup is I have two stream of media one video (with its own audio channel) & one audio.我的设置是我有两个媒体流,一个视频(有自己的音频通道)和一个音频。 I am not converting from but I am restreaming live source by integrating it with an additional audio channel without replacing the old audio from the video stream.我不是从转换过来的,而是通过将它与附加音频通道集成而不替换视频流中的旧音频来重新流式传输实时源。

Here is the code I used.这是我使用的代码。

ffmpeg -i "Video stream with its own audio" -i "another audio stream" -map 0:v -map 0:a -map 1:a -shortest -f mpegts "multicast udp stream out put"

what the code does is, it maps each video and audio channels after it acquired the streams from the live source.代码的作用是,它在从实时源获取流后映射每个视频和音频通道。 -map 0:v is the video stream, -map 0:a is the audio that is coming from the video source (notice the 0s from the -map) and finally -map 1:a which is the audio stream from the second source. -map 0:v是视频流, -map 0:a是来自视频源的音频(注意 -map 中的 0),最后-map 1:a是来自第二个源的音频流.

then it just restreams it using mpegts through a multicast address.然后它只是通过多播地址使用 mpegts 重新流式传输它。 You can change this to a file, a unicast stream or any other supported output format.您可以将其更改为文件、单播流或任何其他支持的输出格式。

Here is the code I am using.这是我正在使用的代码。

ffmpeg -i "rtp://@231.0.0.208:1234" -i "rtp://@231.0.0.206:1234" -map 0:v -map 0:a -map 1:a -shortest -f mpegts "udp://@231.0.0.45:1234"

Hope this helps some one.希望这对某些人有所帮助。 Thanks!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM