Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have two video files that also have audio tracks. I would like to copy the audio track of one file to the other with a 5000msec time offset.

The only problem is that the video files does not have the same duration, so I would like the resulted video files to have blank frames in the beginning/end if the audio starts before or ends after the video.

I'm looking for a command line solution that can be used on linux such as ffmpeg, mencoder etc.

EDIT: run ffmpeg -i 1.mp4 -i 2.mp4

ffmpeg version 0.8.9-4:0.8.9-0ubuntu0.12.04.1, Copyright (c) 2000-2013 the Libav developers
  built on Nov  9 2013 19:08:00 with gcc 4.6.3
*** THIS PROGRAM IS DEPRECATED ***
This program is only provided for compatibility and will be removed in a future release. Please use avconv instead.
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '1.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    creation_time   : 2013-06-15 03:58:00
  Duration: 00:06:49.29, start: 0.000000, bitrate: 3009 kb/s
    Stream #0.0(und): Video: h264 (High), yuv420p, 1280x720, 2814 kb/s, 25 fps, 25 tbr, 50 tbn, 50 tbc
    Stream #0.1(und): Audio: aac, 44100 Hz, stereo, s16, 192 kb/s
    Metadata:
      creation_time   : 2013-06-15 03:58:05
Input #1, mov,mp4,m4a,3gp,3g2,mj2, from '2.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    creation_time   : 2013-06-15 17:25:15
  Duration: 00:06:59.09, start: 0.000000, bitrate: 3159 kb/s
    Stream #1.0(und): Video: h264 (High), yuv420p, 1280x720, 2965 kb/s, 25 fps, 25 tbr, 50 tbn, 50 tbc
    Stream #1.1(und): Audio: aac, 44100 Hz, stereo, s16, 192 kb/s
    Metadata:
      creation_time   : 2013-06-15 17:25:21
At least one output file must be specified
share|improve this question
1  
Can you show some information about the input files and your ffmpeg build? The complete console output of ffmpeg -i input1 -i input2 will suffice. What are the exact offset times you want for each file? –  LordNeckbeard Dec 9 '13 at 22:58
    
@LordNeckbeard see my edit. –  Tal Darom Dec 10 '13 at 12:47
    
Just pointing out- you are using avconv/libav (not the real ffmpeg). When you say offset, you want the audio to start 5000 millisec after video? Then that is contradicting blank frames before audio start. Here one is 00:06:49.29 And the other is 00:06:59.09. Exactly how do you want the audio and video juxtaposed? If you can clarify a little more? –  Rajib Dec 10 '13 at 14:27
    
@Rajib I don't mind what tool to use as long as it will get the job done. I'm currently using the "ffmpeg" that was the default ubuntu 12.04 package. If I'll find a solution that works only with the real ffmpeg I'll use it. Regarding the empty frames, if (because of the offset) the audio should start before the video or end after the video has ended I want there to be blank frames and just audio. –  Tal Darom Dec 10 '13 at 16:55
    
Generally you should use filter graph with filters trim, atrim and concat. And for blank screen you can use lavfi input. You have not answered Rajib's question, so I can't give you example command. –  ptQa Dec 12 '13 at 9:00
show 1 more comment

1 Answer

up vote 2 down vote accepted
+100

Ok. Let's assume you have 1.mp4 - video with with duration 30 sec, 2.mp4 - video with duration 30 sec, and you want to insert audio from 2.mp4 to 1.mp4 video with offset 5 sec. And also you need black video frames when video stream of 1.mp4 ends. So the command should be like:

ffmpeg -i 1.mp4 -i 2.mp4 -f lavfi -i "color=black:size=1920x1080:duration=5" \
-filter_complex \
"[0:v][2:v]concat[video_out]; \
 [0:a]atrim=duration=5[a1]; \
 [a1][1:a]concat=v=0:a=1[audio_out]" \
-map [video_out] -map [audio_out] out.mp4

This command does the following - we take 1.mp4 and 2.mp4 as input, and also generate 5 sec of video with black frames using lavfi. You may have another size of your input videos, so use the same same as 1.mp4 and 2.mp4 for lavfi (change 1920x1080 to whatever size your inputs are).

Then we combine 1.mp4 with black frames from lavfi with concat, then we cut out first 5 sec of audio out of 1.mp4 with atrim and combine it with audio stream from 2.mp4 (concat again). Then just specify that we want the output streams in out.mp4.

Hope it helps.

share|improve this answer
    
Thanks. That was not exactly what I wanted, but close enough for me to do the needed changes by myself. –  Tal Darom Dec 18 '13 at 17:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.