ffmpeg concatenate videos with audio not synched

2019-08-12 09:53发布

问题:

i am trying to concatenate 3 mp4 files into 1. This is the ffmpeg command im making:

shell_exec('ffmpeg -f concat -i '.$random.'.txt -c:v libx264 -preset ultrafast -crf 30 -c:a copy '.$uploadfolder.$random.'.mp4 2>&1');

The 1st and 3rd files are entry and end graphic scenes, which have the above information:

pinicial.mp4

ffprobe version N-76417-gee20354 Copyright (c) 2007-2015 the FFmpeg developers
built with gcc 4.6 (Ubuntu/Linaro 4.6.3-1ubuntu5)
configuration: --prefix=/home/dmarcos89/ffmpeg_build --extra-cflags=-I/home/dmarcos89/ffmpeg_build/include --extra-ldflags=-L/home/dmarcos89/ffmpeg_build/lib --bindir=/usr/local/bin --pkg-config-flags=--static --enable-gpl --enable-nonfree --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265
libavutil      55.  5.100 / 55.  5.100
libavcodec     57. 13.102 / 57. 13.102
libavformat    57. 13.100 / 57. 13.100
libavdevice    57.  0.100 / 57.  0.100
libavfilter     6. 14.101 /  6. 14.101
libswscale      4.  0.100 /  4.  0.100
libswresample   2.  0.100 /  2.  0.100
libpostproc    54.  0.100 / 54.  0.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'http://filtremoscolombia.com/test/videos/placas/pinicio.mp4':
Metadata:
major_brand     : isom
minor_version   : 512
compatible_brands: isomiso2avc1mp41
encoder         : Lavf57.13.100
Duration: 00:00:03.14, start: 0.021333, bitrate: 413 kb/s
Stream #0:0(eng): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 367 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
Metadata:
handler_name    : VideoHandler
Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, 5.1, fltp, 43 kb/s (default)
Metadata:
handler_name    : SoundHandler

pfinal.mp4

ffprobe version N-76417-gee20354 Copyright (c) 2007-2015 the FFmpeg developers
  built with gcc 4.6 (Ubuntu/Linaro 4.6.3-1ubuntu5)
  configuration: --prefix=/home/dmarcos89/ffmpeg_build --extra-cflags=-I/home/dmarcos89/ffmpeg_build/include --extra-ldflags=-L/home/dmarcos89/ffmpeg_build/lib --bindir=/usr/local/bin --pkg-config-flags=--static --enable-gpl --enable-nonfree --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265
  libavutil      55.  5.100 / 55.  5.100
  libavcodec     57. 13.102 / 57. 13.102
  libavformat    57. 13.100 / 57. 13.100
  libavdevice    57.  0.100 / 57.  0.100
  libavfilter     6. 14.101 /  6. 14.101
  libswscale      4.  0.100 /  4.  0.100
  libswresample   2.  0.100 /  2.  0.100
  libpostproc    54.  0.100 / 54.  0.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'http://filtremoscolombia.com/test/videos/placas/pfinal.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf57.13.100
  Duration: 00:00:03.99, start: 0.021333, bitrate: 612 kb/s
    Stream #0:0(eng): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 576 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, 5.1, fltp, 39 kb/s (default)
    Metadata:
      handler_name    : SoundHandler

The "middle" one:

ffprobe version N-76417-gee20354 Copyright (c) 2007-2015 the FFmpeg developers
built with gcc 4.6 (Ubuntu/Linaro 4.6.3-1ubuntu5)
configuration: --prefix=/home/dmarcos89/ffmpeg_build --extra-cflags=-I/home/dmarcos89/ffmpeg_build/include --extra-ldflags=-L/home/dmarcos89/ffmpeg_build/lib --bindir=/usr/local/bin --pkg-config-flags=--static --enable-gpl --enable-nonfree --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265
libavutil      55.  5.100 / 55.  5.100
libavcodec     57. 13.102 / 57. 13.102
libavformat    57. 13.100 / 57. 13.100
libavdevice    57.  0.100 / 57.  0.100
libavfilter     6. 14.101 /  6. 14.101
libswscale      4.  0.100 /  4.  0.100
libswresample   2.  0.100 /  2.  0.100
libpostproc    54.  0.100 / 54.  0.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'http://filtremoscolombia.com/test/upload/videos/vC6RvCf4pn.mp4':
Metadata:
major_brand     : isom
minor_version   : 512
compatible_brands: isomiso2avc1mp41
encoder         : Lavf57.13.100
Duration: 00:00:16.90, start: 0.000000, bitrate: 1185 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 640x352 [SAR 1:1 DAR 20:11], 733 kb/s, 29.96 fps, 30 tbr, 15360 tbn, 60 tbc (default)
Metadata:
handler_name    : VideoHandler
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, 5.1, fltp, 447 kb/s (default)
Metadata:
handler_name    : SoundHandler

And the result of this is the above:

ffprobe version N-76417-gee20354 Copyright (c) 2007-2015 the FFmpeg developers
built with gcc 4.6 (Ubuntu/Linaro 4.6.3-1ubuntu5)
configuration: --prefix=/home/dmarcos89/ffmpeg_build --extra-cflags=-I/home/dmarcos89/ffmpeg_build/include --extra-ldflags=-L/home/dmarcos89/ffmpeg_build/lib --bindir=/usr/local/bin --pkg-config-flags=--static --enable-gpl --enable-nonfree --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265
libavutil      55.  5.100 / 55.  5.100
libavcodec     57. 13.102 / 57. 13.102
libavformat    57. 13.100 / 57. 13.100
libavdevice    57.  0.100 / 57.  0.100
libavfilter     6. 14.101 /  6. 14.101
libswscale      4.  0.100 /  4.  0.100
libswresample   2.  0.100 /  2.  0.100
libpostproc    54.  0.100 / 54.  0.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'http://filtremoscolombia.com/test/upload/videos/lYF00bMR2p.mp4':
Metadata:
major_brand     : isom
minor_version   : 512
compatible_brands: isomiso2avc1mp41
encoder         : Lavf57.13.100
Duration: 00:00:24.05, start: 0.000000, bitrate: 2121 kb/s
Stream #0:0(eng): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 1793 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
Metadata:
handler_name    : VideoHandler
Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, 5.1, fltp, 327 kb/s (default)
Metadata:
handler_name    : SoundHandler

This is the video itself: http://filtremoscolombia.com/test/upload/videos/lYF00bMR2p.mp4

The audio is not well synched and also audio is faster than the original file... Any help would be great

回答1:

The concat demuxer uses the timebase of the first input and does not rescale PTS of 2nd and further inputs. So, your middle file's audio, which is at 44.1 KHz, is being sped up, since the output audio timebase is 1/48000.

Re-encode either the middle to match the entry video or the entry & end videos to match the middle, and then run concat.

If re-encoding middle:

ffmpeg -i vC6RvCf4pn.mp4 -crf 18 -vf scale=1280x720,setsar=1 -profile:v baseline -ar 48000 -strict -2 new-middle.mp4