0

I have a video file, say it's called video.mp4. The video has its own audio. I also have an audio file, say audio.aac, shorter in length than the video.

I want to use ffmpeg to overwrite the audio of video.mp4 with the audio from audio.aac. But since the audio.aac is shorter than video.mp4, after it ends I want the original audio the video had to continue (not from the beginning of course, I don't want to shift it).

Preferably without reencoding.

EDIT: video.mp4 and audio.aac are not files I have. I want to make a script that can work with any input files. Here are some scripts I currently have:

I can extract the audio from the video like this:

ffmpeg -i $1 -vn -acodec copy $2

I can replace the audio on a video with one from another file like this (keeping shortest length):

ffmpeg -i "$1" -i "$2" -vcodec copy -acodec copy -shortest -map 0:v -map 1:a "$3"

I can replace the audio on a video with one from another file like this (without keeping shortest length):

ffmpeg -i "$1" -i "$2" -vcodec copy -acodec copy -map 0:v -map 1:a "$3"

I can trim a video like this (I know it may not be very accurate without reencoding but works for me):

ffmpeg -ss "$1" -i "$2" -ss 0 -c copy -to "$3" -avoid_negative_ts make_zero "$4"

I want to do something like the third script but instead of silence after the audio ends, I want the original audio of the video.

4
  • How far have you gotten, (ie. what is your code so far?) This is really not a place to write code for people, if you show what you have, someone might be able to help you add to it or write it better. (See Related)
    – vssher
    Commented Jun 2, 2020 at 23:08
  • 1
    Run this command: ffmpeg -n -i video.mp4 -i audio.aac. It will provide useful info about the inputs and your ffmpeg version. This info is required to provide an answer that you can copy and paste. Copy the complete log from that command. Edit your question and paste the complete log into your question. Ignore the At least one output file must be specified error in the log.
    – llogan
    Commented Jun 3, 2020 at 0:07
  • @llogan I want this to be a generic script, video.mp4 and audio.aac are theoretical. See my edit for some scripts I already have.
    – mikey_john
    Commented Jun 3, 2020 at 10:24
  • Also @vssher, see my edits
    – mikey_john
    Commented Jun 3, 2020 at 10:24

1 Answer 1

1
  1. Get info about audio.aac including duration.

    ffprobe -v error -show_streams -show_entries stream=codec_name,sample_rate,channels:format=duration audio.aac
    

    Example output:

    [STREAM]
    codec_name=aac
    sample_rate=44100
    channels=2
    [/STREAM]
    [FORMAT]
    duration=29.463936
    [/FORMAT]
    
  2. Conform audio from other input to match attributes of audio.aac:

    ffmpeg -i input.mp4 -ss 29.463936 -map 0:a -c:a aac -ar 44100 -ac 2 conformed.m4a
    
  3. Make input.txt:

    file 'audio.aac'
    file 'conformed.m4a'
    
  4. Concatenate:

    ffmpeg -i input.mp4 -f concat -i input.txt -map 0:v -map 1:a -c copy -movflags +faststart output.mp4
    
4
  • So my files are not completely the same but I don't have an issue with reencoding the main audio file. Is there a quick way to reencode only the part that will be concatenated? And is there a way to automate the conversion to the same format as the other file?
    – mikey_john
    Commented Jun 4, 2020 at 22:15
  • @mikey_john That would have been good to know before answering. Thought no re-encoding was a requirement. Since it seems that all inputs will be random and arbitrary I recommend re-encoding everything so they are all consistent. But that's a whole different answer and I'm feeling too lazy to answer another question.
    – llogan
    Commented Jun 4, 2020 at 22:21
  • I don't see how everything needs to be reencoded. For instance the video won't be concatenated to anything. And you said the audios will have to be the same format so only one of them needs to reencoded to match the other, no? So is there a way to reencode to the same format as the first automatically?
    – mikey_john
    Commented Jun 5, 2020 at 8:12
  • @mikey_john No automatic method. Use ffprobe. See new answer.
    – llogan
    Commented Jun 5, 2020 at 18:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .