FFmpeg - Concatenate videos with not know format

2020-05-06 11:09发布

问题:

I am developing an application. People upload videos from their mobile, from other places.

Using a CMS in PHP (it is the language with which the application is developed) I need to generate a unique video with these partial uploads.

Through FFmpeg I am doing tests, from the command line:

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

This code when I run it says:

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%

This execution generates a video, but not concatenated with the 2 specified, only with the first one.

Why not join the 2?

The videos to upload, will be of very different formats so I can not define codec.

回答1:

You will have to make all inputs similar before concatenation, then use the concat filter. A rough example (you will of course have to customize it to your needs):

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


回答2:

Using this adaptation of code, i can generate a video with two sources.

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

I'm not sure if I can concatenate any video format, from any device of any source / format with this code.