3

I have multiple videos in folder, but when first of all hasn't audio stream, audio disappear in concatenated video. Concatenated video is correct if first video has audio stream. How can I concatenate videos without losing of audio. Can someone help me? Thanks in advance.

It's my piece of code for concatenation

ffmpeg -f concat -i "1.txt" -c copy $OUTPUT_FILE

where "1.txt" file contains list of videos for concatenation

I can omit it by adding of silent audio in first video, if it hasn't audio, but maybe someone know better way?

first video info (from mplayer):

VIDEO:  [H264]  1920x1080  24bpp  30.000 fps  3691.0 kbps (450.6 kbyte/s)
Clip info:
 major_brand: isom
 minor_version: 512
 compatible_brands: isomiso2avc1mp41
 encoder: Lavf56.30.100
Load subtitles in ./
==========================================================================
Opening video decoder: [ffmpeg] FFmpeg's libavcodec codec family
libavcodec version 54.92.100 (external)
Selected video codec: [ffh264] vfm: ffmpeg (FFmpeg H.264)
==========================================================================
Audio: no sound

second video info:

VIDEO:  [H264]  1920x1080  24bpp  30.000 fps  2761.4 kbps (337.1 kbyte/s)
Clip info:
 major_brand: isom
 minor_version: 512
 compatible_brands: isomiso2avc1mp41
 encoder: Lavf56.30.100
Load subtitles in ./
==========================================================================
Opening video decoder: [ffmpeg] FFmpeg's libavcodec codec family
libavcodec version 54.92.100 (external)
Selected video codec: [ffh264] vfm: ffmpeg (FFmpeg H.264)
==========================================================================
==========================================================================
Opening audio decoder: [ffmpeg] FFmpeg/libavcodec audio decoders
AUDIO: 44100 Hz, 2 ch, floatle, 128.0 kbit/4.53% (ratio: 15998->352800)
Selected audio codec: [ffaac] afm: ffmpeg (FFmpeg AAC (MPEG-2/MPEG-4 Audio))

ffmpeg output from first video

ffmpeg version 2.6.git Copyright (c) 2000-2015 the FFmpeg developers
  built with gcc 4.8.3 (GCC) 20140911 (Red Hat 4.8.3-7)
  configuration: --prefix=/root/ffmpeg_build --extra-cflags=-I/root/ffmpeg_build/include --extra-ldflags=-L/root/ffmpeg_build/lib --bindir=/root/bin --enable-gpl --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libvorbis --enable-libvpx --enable-libx264 --enable-nonfree
  libavutil      54. 23.101 / 54. 23.101
  libavcodec     56. 35.101 / 56. 35.101
  libavformat    56. 30.100 / 56. 30.100
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 14.100 /  5. 14.100
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  1.100 /  1.  1.100
  libpostproc    53.  3.100 / 53.  3.100
Input #0, image2, from '10000_full_400511img002.jpg':
  Duration: 00:00:00.04, start: 0.000000, bitrate: 58229 kb/s
    Stream #0:0: Video: mjpeg, yuvj420p(pc, bt470bg/unknown/unknown), 1920x1080 [SAR 1:1 DAR 16:9], 25 fps, 25 tbr, 25 tbn, 25 tbc
[swscaler @ 0x39560e0] deprecated pixel format used, make sure you did set range correctly
[libx264 @ 0x396cb60] -qscale is ignored, -crf is recommended.                                          
[libx264 @ 0x396cb60] using SAR=1/1
[libx264 @ 0x396cb60] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2
[libx264 @ 0x396cb60] profile High, level 4.0
[libx264 @ 0x396cb60] 264 - core 130 r2282 1db4621 - H.264/MPEG-4 AVC codec - Copyleft 2003-2013 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to 'test_output.mp4':
  Metadata:
    encoder         : Lavf56.30.100
    Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], q=-1--1, 30 fps, 15360 tbn, 30 tbc
    Metadata:
      encoder         : Lavc56.35.101 libx264
Stream mapping:
  Stream #0:0 -> #0:0 (mjpeg (native) -> h264 (libx264))
Press [q] to stop, [?] for help
frame=   60 fps= 16 q=-1.0 Lsize=     324kB time=00:00:01.93 bitrate=1374.8kbits/s    
video:323kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.471130%
[libx264 @ 0x396cb60] frame I:1     Avg QP:18.03  size:321845
[libx264 @ 0x396cb60] frame P:15    Avg QP:16.15  size:   338
[libx264 @ 0x396cb60] frame B:44    Avg QP:13.72  size:    70
[libx264 @ 0x396cb60] consecutive B-frames:  1.7%  0.0%  5.0% 93.3%
[libx264 @ 0x396cb60] mb I  I16..4: 10.1% 89.7%  0.3%
[libx264 @ 0x396cb60] mb P  I16..4:  0.0%  0.0%  0.0%  P16..4:  1.6%  0.0%  0.0%  0.0%  0.0%    skip:98.4%
[libx264 @ 0x396cb60] mb B  I16..4:  0.0%  0.0%  0.0%  B16..8:  0.0%  0.0%  0.0%  direct: 0.0%  skip:100.0%  L0:49.1% L1:50.9% BI: 0.0%
[libx264 @ 0x396cb60] 8x8 transform intra:89.4% inter:99.1%
[libx264 @ 0x396cb60] coded y,uvDC,uvAC intra: 88.8% 88.3% 80.7% inter: 0.0% 0.4% 0.0%
[libx264 @ 0x396cb60] i16 v,h,dc,p: 98%  0%  2%  0%
[libx264 @ 0x396cb60] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 14% 21% 19%  6%  7%  6%  9%  8% 11%
[libx264 @ 0x396cb60] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu:  7% 18% 10%  7% 13%  9% 16%  6% 14%
[libx264 @ 0x396cb60] i8c dc,h,v,p: 53% 17% 16% 13%
[libx264 @ 0x396cb60] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0x396cb60] ref P L0: 93.4%  0.0%  3.7%  2.8%
[libx264 @ 0x396cb60] ref B L0: 52.8% 45.3%  1.9%
[libx264 @ 0x396cb60] kb/s:1320.02

Then I built the same second video, and concatenated with audio:

ffmpeg version 2.6.git Copyright (c) 2000-2015 the FFmpeg developers
  built with gcc 4.8.3 (GCC) 20140911 (Red Hat 4.8.3-7)
  configuration: --prefix=/root/ffmpeg_build --extra-cflags=-I/root/ffmpeg_build/include --extra-ldflags=-L/root/ffmpeg_build/lib --bindir=/root/bin --enable-gpl --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libvorbis --enable-libvpx --enable-libx264 --enable-nonfree
  libavutil      54. 23.101 / 54. 23.101
  libavcodec     56. 35.101 / 56. 35.101
  libavformat    56. 30.100 / 56. 30.100
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 14.100 /  5. 14.100
  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 'test_output.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf56.30.100
  Duration: 00:00:02.00, start: 0.000000, bitrate: 1329 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 1322 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
    Metadata:
      handler_name    : VideoHandler
Input #1, mp3, from 'audio.mp3':
  Metadata:
    title           : Time (zaycev.net)
    artist          : ONUKA
    encoder         : Lavf56.30.100
  Duration: 00:00:02.01, start: 0.011995, bitrate: 130 kb/s
    Stream #1:0: Audio: mp3, 44100 Hz, stereo, s16p, 128 kb/s
    Metadata:
      encoder         : Lavc56.35
[mp4 @ 0x2c67040] Codec for stream 0 does not use global headers but container format requires global headers
Output #0, mp4, to 'final.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf56.30.100
    Stream #0:0(und): Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 1322 kb/s, 30 fps, 30 tbr, 15360 tbn, 15360 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1: Audio: aac ([64][0][0][0] / 0x0040), 44100 Hz, stereo, fltp, 128 kb/s
    Metadata:
      encoder         : Lavc56.35.101 aac
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #1:0 -> #0:1 (mp3 (native) -> aac (native))
Press [q] to stop, [?] for help
frame=   60 fps=0.0 q=-1.0 Lsize=     359kB time=00:00:02.02 bitrate=1454.4kbits/s    
video:323kB audio:32kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.959931%

After that I concatenated two videos and got output. ffmpeg program output in case when first video hasn't audio:

  ffmpeg version 2.6.git Copyright (c) 2000-2015 the FFmpeg developers
      built with gcc 4.8.3 (GCC) 20140911 (Red Hat 4.8.3-7)
      configuration: --prefix=/root/ffmpeg_build --extra-cflags=-I/root/ffmpeg_build/include --extra-ldflags=-L/root/ffmpeg_build/lib --bindir=/root/bin --enable-gpl --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libvorbis --enable-libvpx --enable-libx264 --enable-nonfree
      libavutil      54. 23.101 / 54. 23.101
      libavcodec     56. 35.101 / 56. 35.101
      libavformat    56. 30.100 / 56. 30.100
      libavdevice    56.  4.100 / 56.  4.100
      libavfilter     5. 14.100 /  5. 14.100
      libswscale      3.  1.101 /  3.  1.101
      libswresample   1.  1.100 /  1.  1.100
      libpostproc    53.  3.100 / 53.  3.100
    Input #0, concat, from '1.txt':
      Duration: N/A, start: 0.000000, bitrate: 3691 kb/s
        Stream #0:0: Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 3691 kb/s, 30 fps, 30 tbr, 90k tbn, 60 tbc
    [mp4 @ 0x2b5e580] Codec for stream 0 does not use global headers but container format requires global headers
    Output #0, mp4, to 'final.mp4':
      Metadata:
        encoder         : Lavf56.30.100
        Stream #0:0: Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 3691 kb/s, 30 fps, 30 tbr, 90k tbn, 90k tbc
    Stream mapping:
      Stream #0:0 -> #0:0 (copy)
    Press [q] to stop, [?] for help
    [concat @ 0x2b501c0] New audio stream 0:1 at pos:259207 and DTS:12.267s
    frame=  824 fps=0.0 q=-1.0 Lsize=   10660kB time=00:00:27.39 bitrate=3188.2kbits/s    
    video:10651kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.088069%
  • Please show info about each input. The complete output of ffmpeg -i input1 -i input2 -i input3 will suffice. – llogan Jun 17 '15 at 0:07
  • @LordNeckbeard I added info for each video and ffmpeg output. Thanks! – KravAn Jun 17 '15 at 8:16
  • What I'm looking for is the complete ffmpeg output from each video. – llogan Jun 17 '15 at 15:10
  • @LordNeckbeard Sorry, didn't understand at first time. Added ffmpeg outputs from each video. – KravAn Jun 17 '15 at 21:07
1

From the FFmpeg documentation on the concat format:

All files must have the same streams (same codecs, same time base, etc.).

Note that the documentation for the concat filter has similar requirements:

The filter works on segments of synchronized video and audio streams. All segments must have the same number of streams of each type, and that will also be the number of streams at output.

The trick here is that in order for your concat operation to work properly, it's necessary for an audio stream to be present in the first, "silent" video file in order for the audio encoder to be used throughout the whole of your concatenated encode. You can add a silent audio stream in at the time of the first file's creation using the aevalsrc audio source with a value of 0:

ffmpeg \
-f image2 -i [your image-set name info here] \
-f lavfi -i aevalsrc=0 \
-map 0 -map 1 -shortest \
-c:v libx264 [your X264 params here, same settings as in your 2nd file to concat] \
-c:a aac -strict experimental [your aac params here, same settings as in your 2nd file] \
/path/to/output/file/output_file.mp4
  • But I didn't understand why it depends on order of video with and without audio stream :) – KravAn Jun 22 '15 at 10:32
  • Ah... it's because if the first input has no audio stream, FFmpeg won't initialize the an audio encoder (even after it starts chewing on the 2nd input with an audio stream), since you're not supplying anything for the encoder to do at the operation's start. Hope this helps clarify. – Mr. What Jun 22 '15 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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