0

I would like to create a new movie from a JPG image and some movie file using ffmpeg. The image should be displayed for some seconds at the beginning of the movie. This can be achieved using the command

ffmpeg -loop 1 -framerate 60 -t 8 -i image.jpg -t 8 -f lavfi -i aevalsrc=0 -i movie.mp4 -filter_complex '[0:0] [1:0] [2:0] [2:1] concat=n=2:v=1:a=1' output.mp4

Here the framerate is 60fps and the image is shown for 8 seconds. This all works fine, but takes a lot of time as everything is encoded again. Instead I tried to create a movie from the image first using

ffmpeg -loop 1 -framerate 60 -i image.jpg -c:v libx264 -t 8 -pix_fmt yuv420p intro.mp4

and then concatenating the intro to the movie file as described here https://trac.ffmpeg.org/wiki/Concatenate. The second method is significantly faster, but the resulting movie file has no audio. Does anyone know a fast method to do this?

As per the request below, I post ffmpeg -i movie.mp4, it gives

ffmpeg -i movie.mp4                   
ffmpeg version 4.3.1 Copyright (c) 2000-2020 the FFmpeg developers
  built with Apple clang version 12.0.0 (clang-1200.0.32.2)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.3.1_1 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-libsoxr --enable-videotoolbox --disable-libjack --disable-indev=jack
  libavutil      56. 51.100 / 56. 51.100
  libavcodec     58. 91.100 / 58. 91.100
  libavformat    58. 45.100 / 58. 45.100
  libavdevice    58. 10.100 / 58. 10.100
  libavfilter     7. 85.100 /  7. 85.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  7.100 /  5.  7.100
  libswresample   3.  7.100 /  3.  7.100
  libpostproc    55.  7.100 / 55.  7.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'movie.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.45.100
  Duration: 00:01:36.55, start: 0.000000, bitrate: 445 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuvj420p(pc), 1488x1116, 301 kb/s, 60 fps, 60 tbr, 15360 tbn, 120 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 131 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
220284
  • 179
  • 7
  • add audio to `intro.mp4`, both files must have 2 tracks – Баяр Гончикжапов Oct 28 '20 at 14:51
  • Many thanks! Any hints on how to do this would be much appreciated. – 220284 Oct 28 '20 at 15:18
  • You need to make sure `intro.mp4` attributes match those of `movie.mp4`. If you show the output of `ffmpeg -i movie.mp4` it will show detailed file info that I can use to provide a working command you can copy and paste. – llogan Oct 28 '20 at 17:55
  • add silence "-f lavfi -i aevalsrc=0": `ffmpeg -loop 1 -framerate 60 -i image.jpg -f lavfi -i aevalsrc=0 -c:v libx264 -t 8 intro.mp4` – Баяр Гончикжапов Oct 29 '20 at 04:30
  • 1
    Many thanks for the responses! The above command works fine, but produces a movie with mono audio. A movie with stereo audio can be produced with `ffmpeg -loop 1 -framerate 60 -i image.jpg -f lavfi -i anullsrc -c:v libx264 -t 8 intro.mp4` – 220284 Oct 29 '20 at 09:06
  • Hm, it appears that the stereo solution produces a file so that the concatenated movie works fine with VLC, but Quicktime complains and cannot play the file. @llogan The mp4 movie file contains an h264 movie stream and 44.1kHz aac (LC) stereo audio. – 220284 Oct 29 '20 at 09:24
  • @llogan Ok, I have added the output. – 220284 Oct 29 '20 at 19:48

1 Answers1

2
  1. Make intro.mp4: matching attributes of movie.mp4.

    ffmpeg -loop 1 -framerate 60 -t 8 -i image.jpg -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 -filter_complex "[0]scale=1488:1116:force_original_aspect_ratio=increase,crop=1488:1116,setsar=1,format=yuvj420p[v]" -map "[v]" -map 1 -c:v libx264 -c:a aac -shortest intro.mp4
    
  2. Make input.txt containing:

    file 'intro.mp4'
    file 'movie.mp4'
    
  3. Concatenate with concat demuxer:

    ffmpeg -f concat -i input.txt -c copy -movflags +faststart output.mp4
    

See Resizing videos with ffmpeg to fit specific size if you want to pad instead of crop to fit the image to match the video.

llogan
  • 121,796
  • 28
  • 232
  • 243