{"id":4660,"date":"2014-03-30T14:21:42","date_gmt":"2014-03-30T14:21:42","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/concat-2-mp4-files-created-with-ffmpeg-not-working-correctly-collection-of-common-programming-errors\/"},"modified":"2014-03-30T14:21:42","modified_gmt":"2014-03-30T14:21:42","slug":"concat-2-mp4-files-created-with-ffmpeg-not-working-correctly-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/concat-2-mp4-files-created-with-ffmpeg-not-working-correctly-collection-of-common-programming-errors\/","title":{"rendered":"Concat 2 .mp4 files created with ffmpeg not working correctly-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/9d96093408df0463815ff4d9b8a0c1a9?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nslhck<\/p>\n<p>I am trying to concat 2 mp4 files that were both created with ffmpeg however the resulting output file does not contain any audio, can someone please help?<\/p>\n<p>Here&#8217;s my current code:<\/p>\n<pre><code>function appendVideo($input, $input_second, $outputfolder, $outputname) {\n    \/\/ output\n    $output = $outputfolder . $outputname;\n    \/\/ intermediate\n    $randomString1 = substr( \"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\", mt_rand(0, 50) , 1) .substr( md5( time() ), 1);\n    $randomString2 = substr( \"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\", mt_rand(0, 50) , 1) .substr( md5( time() ), 1);\n    $intermediate1 = $outputfolder . \"i1_\".$randomString1.\".mpg\";\n    $intermediate2 = $outputfolder . \"i2_\".$randomString2.\".mpg\";\n    \/\/convert to mpg\n\n    $exec1 = \"ffmpeg -i $input -sameq $intermediate1 2&gt;&amp;1\";\n    $exec2 = \"ffmpeg -i $input_second -sameq $intermediate2 2&gt;&amp;1\";\n\n    exec($exec1,$results1,$return1);\n    exec($exec2,$results2,$return2);\n\n\n    \/\/ join and convert back\n    $exec = \"cat $intermediate1 $intermediate2 | ffmpeg -i - -codec:a copy $output 2&gt;&amp;1\";\n    \/\/delete the intermediate files\n\n\n    exec ($exec,$results,$return);\n\n        if (file_exists ( $intermediate1 )) {\n        unlink ( $intermediate1 );\n    }\n    if (file_exists ( $intermediate2 )) {\n        unlink ( $intermediate2 );\n    }\n\n    return $results;\n\n}\n<\/code><\/pre>\n<p>And this is the result returned from ffmpeg in case it helps:<\/p>\n<pre><code>ffmpeg version 0.10.9 Copyright (c) 2000-2013 the FFmpeg developers\nbuilt on Oct 1 2013 19:47:54 with gcc 4.4.7 20120313 (Red Hat 4.4.7-3)\nconfiguration: --prefix=\/usr --bindir=\/usr\/bin --datadir=\/usr\/share\/ffmpeg --incdir=\/usr\/include\/ffmpeg --libdir=\/usr\/lib64 --mandir=\/usr\/share\/man --arch=x86_64 --extra-cflags='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' --enable-bzlib --disable-crystalhd --enable-gnutls --enable-libass --enable-libcdio --enable-libcelt --enable-libdc1394 --disable-indev=jack --enable-libfreetype --enable-libgsm --enable-libmp3lame --enable-openal --enable-libopenjpeg --enable-libpulse --enable-librtmp --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libv4l2 --enable-libx264 --enable-libxvid --enable-x11grab --enable-avfilter --enable-postproc --enable-pthreads --disable-static --enable-shared --enable-gpl --disable-debug --disable-stripping --shlibdir=\/usr\/lib64 --enable-runtime-cpudetect\nlibavutil 51. 35.100 \/ 51. 35.100\nlibavcodec 53. 61.100 \/ 53. 61.100\nlibavformat 53. 32.100 \/ 53. 32.100\nlibavdevice 53. 4.100 \/ 53. 4.100\nlibavfilter 2. 61.100 \/ 2. 61.100\nlibswscale 2. 1.100 \/ 2. 1.100\nlibswresample 0. 6.100 \/ 0. 6.100\nlibpostproc 52. 0.100 \/ 52. 0.100\nmpeg @ 0x111afc0] Estimating duration from bitrate, this may be inaccurate\nnput #0, mpeg, from 'pipe:':\nDuration: N\/A, start: 1.000000, bitrate: 104985 kb\/s\nStream #0:0[0x1e0]: Video: mpeg1video, yuv420p, 768x432 [SAR 1:1 DAR 16:9], 104857 kb\/s, 30 fps, 30 tbr, 90k tbn, 30 tbc\nStream #0:1[0x1c0]: Audio: mp2, 44100 Hz, stereo, s16, 128 kb\/s\nbuffer @ 0x1137280] w:768 h:432 pixfmt:yuv420p tb:1\/1000000 sar:1\/1 sws_param:\nlibx264 @ 0x11221e0] using SAR=1\/1\nlibx264 @ 0x11221e0] using cpu capabilities: none!\nlibx264 @ 0x11221e0] profile High, level 3.0\nlibx264 @ 0x11221e0] 264 - core 120 r2151 a3f4407 - H.264\/MPEG-4 AVC codec - Copyleft 2003-2011 - 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=12 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\nutput #0, mp4, to 'tmp\/zdee7489be42d6db4bfe9ce8e67da161.mp4':\nMetadata:\nencoder : Lavf53.32.100\nStream #0:0: Video: h264 (![0][0][0] \/ 0x0021), yuv420p, 768x432 [SAR 1:1 DAR 16:9], q=-1--1, 30 tbn, 30 tbc\nStream #0:1: Audio: mp2 (i[0][0][0] \/ 0x0069), 44100 Hz, stereo, 128 kb\/s\nStream mapping:\n    Stream #0:0 -&gt; #0:0 (mpeg1video -&gt; libx264)\n    Stream #0:1 -&gt; #0:1 (copy)\nframe= 46 fps= 0 q=0.0 size= 0kB time=00:00:00.00 bitrate= 0.0kbits\/s \nframe= 66 fps= 60 q=29.0 size= 135kB time=00:00:00.26 bitrate=4141.1kbits\/s \nframe= 79 fps= 49 q=29.0 size= 257kB time=00:00:00.70 bitrate=3011.9kbits\/s \nframe= 93 fps= 44 q=29.0 size= 386kB time=00:00:01.16 bitrate=2711.4kbits\/s \n...\nframe= 105 fps= 39 q=29.0 size= 487kideo:14440kB audio:3140kB global headers:0kB muxing overhead 1.021247%\n[libx264 @ 0x11221e0] frame I:31 Avg QP:18.84 size: 42581\n[libx264 @ 0x11221e0] frame P:3369 Avg QP:21.98 size: 3277\n[libx264 @ 0x11221e0] frame B:2629 Avg QP:27.40 size: 923\n[libx264 @ 0x11221e0] consecutive B-frames: 37.1% 14.2% 0.5% 48.2%\n[libx264 @ 0x11221e0] mb I I16..4: 7.6% 68.7% 23.6%\n[libx264 @ 0x11221e0] mb P I16..4: 0.6% 1.5% 0.2% P16..4: 20.9% 7.1% 5.3% 0.0% 0.0% skip:64.3%\n[libx264 @ 0x11221e0] mb B I16..4: 0.1% 0.1% 0.0% B16..8: 17.2% 3.2% 1.0% direct: 0.7% skip:77.6% L0:39.4% L1:51.8% BI: 8.8%\n[libx264 @ 0x11221e0] 8x8 transform intra:64.8% inter:69.2%\n[libx264 @ 0x11221e0] coded y,uvDC,uvAC intra: 57.6% 72.7% 35.5% inter: 7.6% 11.8% 0.6%\n[libx264 @ 0x11221e0] i16 v,h,dc,p: 41% 21% 16% 21%\n[libx264 @ 0x11221e0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 23% 21% 30% 5% 3% 3% 5% 4% 7%\n[libx264 @ 0x11221e0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 31% 23% 12% 4% 5% 5% 7% 6% 6%\n[libx264 @ 0x11221e0] i8c dc,h,v,p: 44% 24% 25% 7%\n[libx264 @ 0x11221e0] Weighted P-Frames: Y:0.7% UV:0.5%\n[libx264 @ 0x11221e0] ref P L0: 69.8% 12.5% 10.8% 6.8% 0.0%\n[libx264 @ 0x11221e0] ref B L0: 89.9% 8.5% 1.6%\n[libx264 @ 0x11221e0] ref B L1: 97.2% 2.8%\n[libx264 @ 0x11221e0] kb\/s:588.6\n<\/code><\/pre>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/9d96093408df0463815ff4d9b8a0c1a9?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nslhck<\/p>\n<p>I can only guess here as to what you&#8217;re trying to achieve, but the immediate solution to your problem would be to use <code>-c:a aac -strict experimental<\/code> instead of <code>-codec:a copy<\/code>. Your .mpg files use the <code>mp2<\/code> audio codec which is apparently not supported in an MP4 container.<\/p>\n<p>But then again, there are quite a few things wrong here. By converting to <code>.mpg<\/code> without any additional options you&#8217;re creating MPEG-1 video, which either results in horrible quality, or terribly huge files. Also, <code>-sameq<\/code> doesn&#8217;t do what you expect it to do. Please forget that option. Lastly, the double conversion introduces quality loss.<\/p>\n<p>First, update to a recent version of <code>ffmpeg<\/code>. Then, read the guide at How to concatenate (join, merge) media files, especially the part about the <code>concat<\/code> filter. There you could concatenate two files directly without needing intermediates.<\/p>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/076957cb92e53c77fad94060c735ba0f?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nNate<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>slhck I am trying to concat 2 mp4 files that were both created with ffmpeg however the resulting output file does not contain any audio, can someone please help? Here&#8217;s my current code: function appendVideo($input, $input_second, $outputfolder, $outputname) { \/\/ output $output = $outputfolder . $outputname; \/\/ intermediate $randomString1 = substr( &#8220;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ&#8221;, mt_rand(0, 50) , [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4660","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4660","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4660"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4660\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}