FFmpeg - 连接不知道格式的视频

时间:2017-03-17 22:07:51

标签: shell ffmpeg concatenation

我正在开发一个应用程序。 人们从其他地方上传手机中的视频。

在PHP中使用CMS(它是开发应用程序的语言)我需要使用这些部分上传来生成一个独特的视频。

通过FFmpeg我正在从命令行进行测试:

ffmpeg -i concat:IMG_1916.mp4\|IMG_1917.mp4 -c copy videoLoop.mp4

我运行时的代码说:

ffmpeg version 3.2.4 Copyright (c) 2000-2017 the FFmpeg developers

built with Apple LLVM version 8.0.0 (clang-800.0.42.1)

configuration: --prefix=/usr/local/Cellar/ffmpeg/3.2.4 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-libmp3lame --enable-libx264 --enable-libxvid --enable-opencl --disable-lzma --enable-vda
    libavutil      55. 34.101 / 55. 34.101
    libavcodec     57. 64.101 / 57. 64.101
    libavformat    57. 56.101 / 57. 56.101
    libavdevice    57.  1.100 / 57.  1.100
    libavfilter     6. 65.100 /  6. 65.100
    libavresample   3.  1.  0 /  3.  1.  0
    libswscale      4.  2.100 /  4.  2.100
    libswresample   2.  3.100 /  2.  3.100
    libpostproc    54.  1.100 / 54.  1.100

[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f8515000000] Found duplicated MOOV Atom. Skipped it Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'concat:IMG_1916.mp4|IMG_1917.mp4':
Metadata:

    encoder         : Lavf57.66.102
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41

Duration: 00:00:04.27, start: 0.000000, bitrate: 26792 kb/s

    Stream #0:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 1920x1080, 11978 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 59.94 tbc (default)

Metadata:

    handler_name    : VideoHandler

Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, mono, fltp, 120 kb/s (default)
Metadata:

    handler_name    : SoundHandler

Output #0, mp4, to 'videoLoop.mp4':

Metadata:

    compatible_brands: isomiso2avc1mp41
    major_brand     : isom
    minor_version   : 512
    encoder         : Lavf57.56.101
    Stream #0:0(und): Video: h264 (Constrained Baseline) ([33][0][0][0] / 0x0021), yuv420p, 1920x1080, q=2-31, 11978 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 30k tbc (default)

Metadata:

    handler_name    : VideoHandler

Stream #0:1(und): Audio: aac (LC) ([64][0][0][0] / 0x0040), 44100 Hz, mono, 120 kb/s (default)
Metadata:

    handler_name    : SoundHandler

Stream mapping:

    Stream #0:0 -> #0:0 (copy)
    Stream #0:1 -> #0:1 (copy)

Press [q] to stop, [?] for help
frame=  127 fps=0.0 q=-1.0 Lsize=    6264kB time=00:00:04.22 bitrate=12142.8kbits/s speed= 376x    
video:6196kB audio:63kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.076698%

此执行生成视频,但不与指定的2连接,仅与第一个连接。

为什么不加入2?

要上传的视频格式会有很大差异,因此我无法定义编解码器。

3 个答案:

答案 0 :(得分:2)

在连接之前,您必须使所有输入相似,然后使用concat filter。一个粗略的例子(你当然必须根据你的需要定制它):

ffmpeg -i input0 -i input1 -filter_complex \
"[0:v]fps=25,scale=1280:720,format=yuv420p,setsar=1,setpts=PTS-STARTPTS[v0]; \
 [1:v]fps=25,scale=1280:720,format=yuv420p,setsar=1,setpts=PTS-STARTPTS[v1]; \
 [0:a]aformat=channel_layouts=stereo:sample_rates=44100,asetpts=PTS-STARTPTS[a0]; \
 [1:a]aformat=channel_layouts=stereo:sample_rates=44100,asetpts=PTS-STARTPTS[a1]; \
 [v0][a0][v1][a1]concat=n=2:v=1:a=1[v][a]" \
-map "[v]" -map "[a]" -c:v libx264 -c:a aac -movflags +faststart output.mp4

答案 1 :(得分:0)

使用这种代码调整,我可以生成一个包含两个源的视频。

ffmpeg -i IMG_1916.mp4 -i IMG_1917.mp4 \
-filter_complex \
"[0:v:0] [0:a:0] \
[1:v:0] [1:a:0] \
concat=n=2:v=1:a=1 [v] [a]" \
-map "[v]" -map "[a]" videoLoop.mp4

我不确定是否可以使用此代码从任何源/格式的任何设备连接任何视频格式。

答案 2 :(得分:0)

使用此代码,我可以毫无问题地创建视频连接。

/usr/bin/ffmpeg -i file1.mp4 -i file2.mp4 -filter_complex \
    \"[0:v]fps=25,scale=1280:720,format=yuv420p,setsar=1,setpts=PTS-STARTPTS[v0]; \
     [1:v]fps=25,scale=1280:720,format=yuv420p,setsar=1,setpts=PTS-STARTPTS[v1]; \
     [0:a]aformat=channel_layouts=stereo:sample_rates=44100,asetpts=PTS-STARTPTS[a0]; \
     [1:a]aformat=channel_layouts=stereo:sample_rates=44100,asetpts=PTS-STARTPTS[a1]; \
     [v0][a0][v1][a1]concat=n=2:v=1:a=1[v][a]\" \
    -map \"[v]\" -map \"[a]\" -c:v libx264 -c:a aac -movflags +faststart output.mp4

使用iPhone 6(MOV格式)加入录制的视频时,问题出现了。

执行失败,此消息“匹配无流”。

附上执行的输出:

    array(52) {
    [0]=>
    string(107) "ffmpeg version 3.2.4-static http://johnvansickle.com/ffmpeg/  Copyright (c) 2000-2017 the FFmpeg developers"
    [1]=>
    string(48) "  built with gcc 5.4.1 (Debian 5.4.1-5) 20170205"
    [2]=>
    string(622) "  configuration: --enable-gpl --enable-version3 --enable-static --disable-debug --disable-ffplay --disable-indev=sndio --disable-outdev=sndio --cc=gcc-5 --enable-fontconfig --enable-frei0r --enable-gnutls --enable-gray --enable-libass --enable-libfreetype --enable-libfribidi --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libzimg"
    [3]=>
    string(40) "  libavutil      55. 34.101 / 55. 34.101"
    [4]=>
    string(40) "  libavcodec     57. 64.101 / 57. 64.101"
    [5]=>
    string(40) "  libavformat    57. 56.101 / 57. 56.101"
    [6]=>
    string(40) "  libavdevice    57.  1.100 / 57.  1.100"
    [7]=>
    string(40) "  libavfilter     6. 65.100 /  6. 65.100"
    [8]=>
    string(40) "  libswscale      4.  2.100 /  4.  2.100"
    [9]=>
    string(40) "  libswresample   2.  3.100 /  2.  3.100"
    [10]=>
    string(40) "  libpostproc    54.  1.100 / 54.  1.100"
    [11]=>
    string(115) "Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'videoLoop.mp4':"
    [12]=>
    string(11) "  Metadata:"
    [13]=>
    string(26) "    major_brand     : isom"
    [14]=>
    string(25) "    minor_version   : 512"
    [15]=>
    string(39) "    compatible_brands: isomiso2avc1mp41"
    [16]=>
    string(35) "    encoder         : Lavf57.56.101"
    [17]=>
    string(60) "  Duration: 00:00:44.20, start: 0.000000, bitrate: 1761 kb/s"
    [18]=>
    string(154) "    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 1630 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)"
    [19]=>
    string(13) "    Metadata:"
    [20]=>
    string(36) "      handler_name    : VideoHandler"
    [21]=>
    string(101) "    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 124 kb/s (default)"
    [22]=>
    string(13) "    Metadata:"
    [23]=>
    string(36) "      handler_name    : SoundHandler"
    [24]=>
    string(155) "Input #1, mov,mp4,m4a,3gp,3g2,mj2, from '51180919245__617D354A-BB50-44AA-B7C1-2052F4EE49EF.MOV':"
    [25]=>
    string(11) "  Metadata:"
    [26]=>
    string(24) "    major_brand     : qt"
    [27]=>
    string(23) "    minor_version   : 0"
    [28]=>
    string(25) "    compatible_brands: qt"
    [29]=>
    string(49) "    creation_time   : 2017-03-21T17:13:12.000000Z"
    [30]=>
    string(35) "    com.apple.quicktime.make: Apple"
    [31]=>
    string(39) "    com.apple.quicktime.model: iPhone 6"
    [32]=>
    string(40) "    com.apple.quicktime.software: 10.2.1"
    [33]=>
    string(62) "    com.apple.quicktime.creationdate: 2017-03-21T18:13:12+0100"
    [34]=>
    string(59) "  Duration: 00:00:11.38, start: 0.000000, bitrate: 714 kb/s"
    [35]=>
    string(168) "    Stream #1:0(und): Video: h264 (Baseline) (avc1 / 0x31637661), yuv420p(tv, smpte170m/bt709/bt709), 480x360, 700 kb/s, 28.31 fps, 600 tbr, 600 tbn, 1200 tbc (default)"
    [36]=>
    string(13) "    Metadata:"
    [37]=>
    string(26) "      rotate          : 90"
    [38]=>
    string(51) "      creation_time   : 2017-03-21T17:13:12.000000Z"
    [39]=>
    string(47) "      handler_name    : Core Media Data Handler"
    [40]=>
    string(29) "      encoder         : H.264"
    [41]=>
    string(14) "    Side data:"
    [42]=>
    string(47) "      displaymatrix: rotation of -90.00 degrees"
    [43]=>
    string(70) "    Stream #1:1(und): Data: none (mebx / 0x7862656D), 5 kb/s (default)"
    [44]=>
    string(13) "    Metadata:"
    [45]=>
    string(51) "      creation_time   : 2017-03-21T17:13:12.000000Z"
    [46]=>
    string(47) "      handler_name    : Core Media Data Handler"
    [47]=>
    string(70) "    Stream #1:2(und): Data: none (mebx / 0x7862656D), 0 kb/s (default)"
    [48]=>
    string(13) "    Metadata:"
    [49]=>
    string(51) "      creation_time   : 2017-03-21T17:13:12.000000Z"
    [50]=>
    string(47) "      handler_name    : Core Media Data Handler"
    [51]=>
    string(491) "Stream specifier ':a' in filtergraph description [0:v]fps=25,scale=1280:720,format=yuv420p,setsar=1,setpts=PTS-STARTPTS[v0];                  [1:v]fps=25,scale=1280:720,format=yuv420p,setsar=1,setpts=PTS-STARTPTS[v1];                  [0:a]aformat=channel_layouts=stereo:sample_rates=44100,asetpts=PTS-STARTPTS[a0];                  [1:a]aformat=channel_layouts=stereo:sample_rates=44100,asetpts=PTS-STARTPTS[a1];                  [v0][a0][v1][a1]concat=n=2:v=1:a=1[v][a] matches no streams."
}
int(1)