Convert mp3 -> Video with static image ( ffmpeg/libav & BASH )

27,859

Solution 1

First of all, I would highly recommend you to update ffmpeg to a newer version. You are currently using v1.2.6-7, but as we speak, the current version is 2.3.3.

Here is a working command (see ffmpeg wiki):

ffmpeg -loop 1 -i image.jpg -i audio.mp3 -c:a copy -c:v libx264 -shortest out.mp4

Also, you could optimize overall weight by reencoding audio if you're using raw sound (aiff or wave) as input:

ffmpeg -loop 1 -i image.jpg -i audio.wav -c:a aac -ab 112k -c:v libx264 -shortest -strict -2 out.mp4

For further reading, see this related stackoverflow post.

Solution 2

ffmpeg -loop 1 -i image.jpg -i audio.mp3 -shortest -c:v libx264 -c:a copy result.mkv 

works with version 3.4.2

Share:
27,859

Related videos on Youtube

Max
Author by

Max

Software Engineer at Technical University Munich. Web Development, Python, Django, Go, SRE, Kubernetes, in the past also a lot of Android and iOS.

Updated on July 09, 2022

Comments

  • Max
    Max almost 2 years

    Some years ago I used the following bash script to get a video out of mp3's and one image (so the image in the video was freezed over the length of the mp3), which worked out fine.

    i=0;
    for file in *.mp3; 
    do 
    i=$((i+1));
    ffmpeg -loop 1 -shortest -y -i image.jpg -i $file -acodec copy -vcodec libx264 $file.flv;
    done
    

    Now I wanted to use this again for doing the same.

    Problem:

    It does not stop converting at the end of the mp3. Means the current mp3 file eg. has a length of 3 minutes and the script converts until forever or I stop it, so the length of the flv is much bigger than 3 minutes.

    (ffmpeg is installed, but with libav the same happens)

    Output:

    ffmpeg version 1.2.6-7:1.2.6-1~trusty1 Copyright (c) 2000-2014 the FFmpeg developers
      built on Apr 26 2014 18:52:58 with gcc 4.8 (Ubuntu 4.8.2-19ubuntu1)
      configuration: --arch=amd64 --disable-stripping --enable-avresample --enable-pthreads --enable-runtime-cpudetect --extra-version='7:1.2.6-1~trusty1' --libdir=/usr/lib/x86_64-linux-gnu --prefix=/usr --enable-bzlib --enable-libdc1394 --enable-libfreetype --enable-frei0r --enable-gnutls --enable-libgsm --enable-libmp3lame --enable-librtmp --enable-libopencv --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-vaapi --enable-vdpau --enable-libvorbis --enable-libvpx --enable-zlib --enable-gpl --enable-postproc --enable-libcdio --enable-x11grab --enable-libx264 --shlibdir=/usr/lib/x86_64-linux-gnu --enable-shared --disable-static
      libavutil      52. 18.100 / 52. 18.100
      libavcodec     54. 92.100 / 54. 92.100
      libavformat    54. 63.104 / 54. 63.104
      libavdevice    53.  5.103 / 53.  5.103
      libavfilter     3. 42.103 /  3. 42.103
      libswscale      2.  2.100 /  2.  2.100
      libswresample   0. 17.102 /  0. 17.102
      libpostproc    52.  2.100 / 52.  2.100
    [image2 @ 0x6d0740] max_analyze_duration 5000000 reached at 5000000 microseconds
    Input #0, image2, from 'Bild2.png':
      Duration: 00:00:00.04, start: 0.000000, bitrate: N/A
        Stream #0:0: Video: png, rgba, 382x417, 25 fps, 25 tbr, 25 tbn, 25 tbc
    [mp3 @ 0x6c4ac0] max_analyze_duration 5000000 reached at 5015510 microseconds
    [mp3 @ 0x6c4ac0] Estimating duration from bitrate, this may be inaccurate
    Input #1, mp3, from 'sons.mp3':
      Duration: 00:00:35.11, start: 0.000000, bitrate: 127 kb/s
        Stream #1:0: Audio: mp3, 44100 Hz, stereo, s16p, 128 kb/s
    [libx264 @ 0x6da700] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
    [libx264 @ 0x6da700] profile High 4:4:4 Predictive, level 2.1, 4:4:4 8-bit
    [libx264 @ 0x6da700] 264 - core 142 r2389 956c8d8 - H.264/MPEG-4 AVC codec - Copyleft 2003-2014 - 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=4 threads=6 lookahead_threads=1 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
    Output #0, flv, to 'sons.mp3.flv':
      Metadata:
        encoder         : Lavf54.63.104
        Stream #0:0: Video: h264 ([7][0][0][0] / 0x0007), yuv444p, 382x417, q=-1--1, 1k tbn, 25 tbc
        Stream #0:1: Audio: mp3 ([2][0][0][0] / 0x0002), 44100 Hz, stereo, 128 kb/s
    Stream mapping:
      Stream #0:0 -> #0:0 (png -> libx264)
      Stream #1:0 -> #0:1 (copy)
    Press [q] to stop, [?] for help
    
  • Max
    Max almost 10 years
    Thanks mrgnou. Version 1.2 is what you get by installing through the ppa mentioned on ffmpeg.org (launchpad.net/~jon-severinsson/+archive/ubuntu/ffmpeg).
  • neoDev
    neoDev about 8 years
    @gnou The mp4 does not work on iphone... please help
  • gnou
    gnou about 8 years
    @neoDev You need to encode the audio channel in aac in that case, so use aac audio codec or libfdk_aac for a better quality (if your ffmpeg is compiled with it).
  • ESP32
    ESP32 over 5 years
    Works well, but very slow (about real time for a still image). Why?
  • ESP32
    ESP32 over 5 years
    In the end I get a 400 MB file from a 30 min mp3 (20 MB) and a still image.
  • SuperMar1o
    SuperMar1o over 3 years
    Pro-Tip. The smaller the image you use as your "Image" file, the faster the conversion will be. I am using a 1px by 1px white image and getting 700x speedup. Also, filesize is very similar. For example, a 37mb m4a becomes a 38.5mb mp4. However, if you are doing this for YouTube, they do not like a 1px white image lol. So I opted for a 640x400 minimalist wallpaper (10KB) and am getting 60x. Good enough for now.
  • Andrew Grow
    Andrew Grow over 2 years
    On Ubuntu 21.04 the solution still works. Thank you Leo!
  • msalihbindak
    msalihbindak about 2 years
    @gnou I use wav instead of mp3 with second code