11

I'm building Electron app and I use ffmpeg to convert m4a or webm files to mp3, and also to merge video only mp4 with m4a audio file to mp4.

I am able to achieve this using [media-autobuild-suite] (https://github.com/jb-alvarado/media-autobuild_suite), using light build option, but the size of static files is arround 20mb and I'would like to shrink it a little bit more. I've compiled ffmpeg and ffprobe with this configuration.

--disable-libaom
--disable-version3
# Full
--disable-chromaprint
--disable-cuda-sdk
--disable-decklink
--disable-frei0r
--disable-libbs2b
--disable-libcaca
--disable-libcdio
--disable-libfdk-aac
--disable-libflite
--disable-libfribidi
--disable-libgme
--disable-libgsm
--disable-libilbc
--disable-libkvazaar
--disable-libmodplug
--disable-libnpp
--disable-libopenh264
--disable-libopenmpt
--disable-librtmp
--disable-librubberband
--disable-libssh
--disable-libtesseract
--disable-libxavs
--disable-libzmq
--disable-libzvbi
--disable-opencl
--disable-opengl
--disable-libvmaf
--disable-libcodec2
--disable-libsrt
--disable-ladspa
--disable-ffplay
#--enable-vapoursynth
#--enable-liblensfun
--disable-libndi_newtek

--enable-demuxer=mp3
--enable-demuxer=mov
--enable-demuxer=opus

--enable-parser=ac3
--enable-parser=mpegaudio
--enable-parser=h264
--enable-parser=opus

--enable-protocol=file
--enable-protocol=pipe

--enable-decoder=mp3
--enable-decoder=mp4
--enable-decoder=opus

--enable-encoder=mp3
--enable-encoder=mp4
--enable-encoder=opus

With this configuration I'm getting ffmpeg static file arround 2mb and ffprobe static file arround 2mb but with this error.

C:\Users\Admin\Desktop\ffmpeg compilations\2mb\local64>ffmpeg -i simple.m4a simple.mp3 
ffmpeg version N-93147-g9326117bf6 Copyright (c) 2000-2019 the FFmpeg developers built with gcc 8.2.1 (Rev1, Built by MSYS2 project) 20181214
configuration:  .... //here comes configuration as described above
libavutil      56. 26.100 / 56. 26.100
libavcodec     58. 47.102 / 58. 47.102
libavformat    58. 26.101 / 58. 26.101
libavdevice    58.  6.101 / 58.  6.101
libavfilter     7. 48.100 /  7. 48.100
libswscale      5.  4.100 /  5.  4.100
libswresample   3.  4.100 /  3.  4.100
libpostproc    55.  4.100 / 55.  4.100
Guessed Channel Layout for Input Stream #0.0 : stereo
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'simple.m4a':
Metadata:
  major_brand     : dash
  minor_version   : 0
  compatible_brands: iso6mp41
  creation_time   : 2018-10-31T19:47:32.000000Z
Duration: 00:02:38.92, start: 0.000000, bitrate: 127 kb/s
Stream #0:0(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, stereo, 7 kb/s (default)
Metadata:
  creation_time   : 2018-10-31T19:47:32.000000Z
  handler_name    : SoundHandler
[NULL @ 0000000000486200] Unable to find a suitable output format for 'simple.mp3' 
simple.mp3: Invalid argument

Any ideas what else should I include into this static file?

Enis Jasarovic
  • 125
  • 1
  • 8

1 Answers1

18

No need to disable things piecemeal: just use --disable-everything then enable what you need.

Here's an example you can start out with:

./configure
--disable-everything
--disable-network
--disable-autodetect
--enable-small
--enable-decoder=aac*,ac3*,opus,vorbis
--enable-demuxer=mov,m4v,matroska
--enable-muxer=mp3,mp4
--enable-protocol=file
--enable-libshine
--enable-encoder=libshine
--enable-filter=aresample
  • Final binary size will be around 2-3 MB.
  • No need to enable any parsers: the selected decoders will automatically select whichever are required.
  • FFmpeg does not have a native MP3 encoder, so you'll need to use an external library such as libmp3lame or libshine. Since you mentioned Android I assumed you'll want libshine instead of libmp3lame to encode MP3.
  • Test thoroughly. I probably forgot something.
  • If you really want to go nuts then use --disable-all instead of --disable-everything and you'll additionally have to manually include the FFmpeg libraries that you want, but that's more work and more headache for not much return.

This will allow you to encode MP3 audio from most M4A and Webm inputs:

ffmpeg -i input.webm output.mp3

And will also let you re-mux MP4/M4V + M4A into MP4:

ffmpeg -i video.m4v -i audio.m4a -map 0:v -map 0:a -c copy output.mp4
llogan
  • 121,796
  • 28
  • 232
  • 243
  • Thanks man, it worked. Actually for those who may need this solution, I have had to enable libmp3lame also, because I have to use this on windows platform. Once again thanks... P.S. I would hit vote up, but my reputation is not enough to do it :) – Enis Jasarovic Feb 19 '19 at 16:14
  • Ok, it's not directly connected to previous question, but I thought to ask you here, what should I have to add to be able to trim videos, for example with this command: "ffmpeg -i input.mp3 -ss 00:00:01 -to 00:00:10 -async 1 output.mp3" – Enis Jasarovic Apr 01 '19 at 17:50
  • @EnisJasarovic Does it not work with the above configuration? If not, what's the error message? Why do you want `-async 1`? – llogan Apr 01 '19 at 18:10
  • no it doesnt. It gives me an folowing error: "moov atom not found" – Enis Jasarovic Apr 01 '19 at 18:23
  • @EnisJasarovic I would not expect that message with MP3 inputs, but maybe I'm missing something. – llogan Apr 01 '19 at 19:09
  • No, sorry, I'm getting that error with mp4 input, with mp3 input I'm getting "invalid data found when processing input", but everything works with large binary from ffmpeg site – Enis Jasarovic Apr 01 '19 at 20:19
  • @EnisJasarovic Minimal builds are tricky. My answer provided a configuration to only do as asked in the original question which did not request decoding (only demuxing/muxing) of video formats in MP4, so you will need to add that. Also, you probably have to take into account of the album/cover art images for MP3, so you'll need to enable components to deal with JPEG and PNG. – llogan Apr 01 '19 at 20:39
  • @llogan hi, i want to only need mix 2 mp3 files to one mp3 files. how to write the compile code ? thanks – badboy_tqj Jul 16 '20 at 12:36
  • @纯洁的坏蛋 Ask new question. What should happen to album image in MP3? I'm assuming you want to use the amix or amerge filters, right? – llogan Jul 16 '20 at 18:06
  • Hi, I wonder whether you get the "2-3MB" *uncompressed* .so file, or *compressed* file? Thank you very much! – ch271828n Sep 17 '20 at 12:30
  • @ch271828n I was referring to the `ffmpeg` executable. – llogan Sep 17 '20 at 17:32