I am using ffmpeg to split out the audio from a video file, converting it to a wav, and then recombining it with the video. When I do this, the new video has black frames at the very beginning.

Here are the commands I am running:

ffmpeg -i orig.mp4 orig-sound.wav 
ffmpeg -i orig.mp4 -i orig-sound.wav -map 0:0 -map 1:0 -vcodec copy new.mp4

I have noticed that if I add an -itsoffset -0.5 when recombining then the black frames are not present, but I don't understand why this works or why it is necessary.

ffmpeg -itsoffset -0.5 -i orig.mp4 -i orig-sound.wav -map 0:0 -map 1:0 -vcodec copy new.mp4

More info: this only happens for some videos.

Split command output

Running command: ffmpeg  -i shortvideo.mp4 -y /var/folders/rr/x5blbrq14yj6zwlj5_y2y5gh0000gp/T/11555-44766-13uwbwy.wav
ffmpeg version 2.6.2 Copyright (c) 2000-2015 the FFmpeg developers
  built with Apple LLVM version 6.1.0 (clang-602.0.49) (based on LLVM 3.6.0svn)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/2.6.2 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-libx264 --enable-libmp3lame --enable-libvo-aacenc --enable-libxvid --enable-vda
  libavutil      54. 20.100 / 54. 20.100
  libavcodec     56. 26.100 / 56. 26.100
  libavformat    56. 25.101 / 56. 25.101
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 11.102 /  5. 11.102
  libavresample   2.  1.  0 /  2.  1.  0
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  1.100 /  1.  1.100
  libpostproc    53.  3.100 / 53.  3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'shortvideo.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf56.25.101
  Duration: 00:01:15.72, start: 0.072562, bitrate: 171 kb/s
    Stream #0:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 800x600, 40 kb/s, 24.01 fps, 24 tbr, 12288 tbn, 48 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 22050 Hz, stereo, fltp, 127 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
Output #0, wav, to '/var/folders/rr/x5blbrq14yj6zwlj5_y2y5gh0000gp/T/11555-44766-13uwbwy.wav':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    ISFT            : Lavf56.25.101
    Stream #0:0(und): Audio: pcm_s16le ([1][0][0][0] / 0x0001), 22050 Hz, stereo, s16, 705 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      encoder         : Lavc56.26.100 pcm_s16le
Stream mapping:
  Stream #0:1 -> #0:0 (aac (native) -> pcm_s16le (native))
Press [q] to stop, [?] for help
size=    6524kB time=00:01:15.74 bitrate= 705.6kbits/s
video:0kB audio:6524kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.001168%

Merge Output

Running command: ffmpeg  -i shortvideo.mp4 -i /var/folders/rr/x5blbrq14yj6zwlj5_y2y5gh0000gp/T/11555-44766-ou67bd.wav -map 0:0 -map 1:0 -vcodec copy -ab 127166 /var/folders/rr/x5blbrq14yj6zwlj5_y2y5gh0000gp/T/11555-44766-1lafn4z.mp4
ffmpeg version 2.6.2 Copyright (c) 2000-2015 the FFmpeg developers
  built with Apple LLVM version 6.1.0 (clang-602.0.49) (based on LLVM 3.6.0svn)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/2.6.2 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-libx264 --enable-libmp3lame --enable-libvo-aacenc --enable-libxvid --enable-vda
  libavutil      54. 20.100 / 54. 20.100
  libavcodec     56. 26.100 / 56. 26.100
Running command: ffmpeg  -i shortvideo.mp4 -y /var/folders/rr/x5blbrq14yj6zwlj5_y2y5gh0000gp/T/11555-44766-13uwbwy.wav
  libavformat    56. 25.101 / 56. 25.101
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 11.102 /  5. 11.102
  libavresample   2.  1.  0 /  2.  1.  0
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  1.100 /  1.  1.100
  libpostproc    53.  3.100 / 53.  3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'shortvideo.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf56.25.101
  Duration: 00:01:15.72, start: 0.072562, bitrate: 171 kb/s
    Stream #0:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 800x600, 40 kb/s, 24.01 fps, 24 tbr, 12288 tbn, 48 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 22050 Hz, stereo, fltp, 127 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
Guessed Channel Layout for  Input Stream #1.0 : stereo
Input #1, wav, from '/var/folders/rr/x5blbrq14yj6zwlj5_y2y5gh0000gp/T/11555-44766-ou67bd.wav':
  Duration: 00:01:15.74, bitrate: 705 kb/s
    Stream #1:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 22050 Hz, 2 channels, s16, 705 kb/s
Output #0, mp4, to '/var/folders/rr/x5blbrq14yj6zwlj5_y2y5gh0000gp/T/11555-44766-1lafn4z.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf56.25.101
    Stream #0:0(und): Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 800x600, q=2-31, 40 kb/s, 24.01 fps, 24 tbr, 12288 tbn, 12288 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1: Audio: aac (libvo_aacenc) ([64][0][0][0] / 0x0040), 22050 Hz, stereo, s16, 127 kb/s
    Metadata:
      encoder         : Lavc56.26.100 libvo_aacenc
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #1:0 -> #0:1 (pcm_s16le (native) -> aac (libvo_aacenc))
Press [q] to stop, [?] for help
[mp4 @ 0x7fbf0c826e00] Packet with invalid duration -2654 in stream 0164.3kbits/s
frame= 1807 fps=0.0 q=-1.0 Lsize=    1585kB time=00:01:15.76 bitrate= 171.4kbits/s
video:376kB audio:1177kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 2.046465%
1/1 videos completed...
Success: shortvideo.mp4
Processed 1623247 bytes in 3.064000129699707 seconds, at 0.5297803300547149 MB/sec
  • Can you explain what you are trying to accomplish as an end goal here? ... What is the point of creating the .wav output? .... There is likely a far better approach, and knowing a bit more info can probably help determine a better solution for you. – occvtech Jun 3 '15 at 17:50
  • Well there is another step I am going to insert in between these two steps - I will be adding audio watermarking to the audio. But I am getting the black frames even without this step. In order to add the watermarking I must have the file in a wav format first. – Keith Johnston Jun 3 '15 at 17:52
  • I'm not very good with audio sync issues... but a couple things you can try until someone more knowledgeable shows up: In your original command, try encoding the audio with -c:a pcm_s16l3. If that doesn't work, you could try resetting the PTS of the video in your second command, but that would require using a video filter (which in turn requires reencoding the video and won't allow you to stream copy). – occvtech Jun 3 '15 at 18:39
  • Please include the complete console output from each command. – llogan Jun 3 '15 at 19:06

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.