Convert recursively in subdirectories
Here is a simple find
version that converts all .mp3
in the current directory and its subdirectories to mp4:
find . -iname '*.mp3' | sort | while IFS="" read -r f; do
ffmpeg \
-nostdin \
-loop 1 \
-r 1 \
-i image.jpg \
-i "$f" \
-vcodec libx264 \
-acodec copy \
-shortest \
"$(basename "${f%.*}.mp4")" \
;
done
Obtain some test data:
wget -O image.jpg https://upload.wikimedia.org/wikipedia/commons/8/83/Lama_Portrait_06072007_01.jpg
wget -O audio.ogg https://upload.wikimedia.org/wikipedia/commons/4/4f/BackUpAndPush.ogg
ffmpeg -i audio.ogg audio.mp3
Uploaded output: https://www.youtube.com/watch?v=tv3a6kP6BDA
If ffmpeg
complains that the image size is not a multiple of 2 (required for libx264
), you can first find the image size with Imagemagick:
identify image.jpg
and then round both width and height down to be even, e.g. if the size were 1023x513 we would use:
convert image.jpg -resize 1024x512! imag2.jpg
where the !
forces that size to be used: https://unix.stackexchange.com/questions/319504/force-graphicsmagick-to-resize-image-to-specific-width
The -nostdin
is required because ffmpeg reads bytes from stdin otherwise, and breaks our read
!!! https://stackoverflow.com/questions/52374260/bash-variable-changes-in-loop-with-ffmpeg
The -vcodec libx264
drastically reduces the final video size to be much closer to that of the original MP3: the default video codec appears to be much worse. It might be possible to reduce the increase in size further by having less keyframes: https://stackoverflow.com/questions/30979714/how-to-change-keyframe-interval-in-ffmpeg In our sample data, we had +3 MB on a 3 minute 3 MB MP3 with a 1.1 MB image.
See also: Combine one image + one audio file to make one video using FFmpeg
Procedural video representation of audio
ffmpeg is very featureful, and you can generate several video representations of your audio using with commands of the form:
ffmpeg -i audio.mp3 \
-filter_complex "[0:a]avectorscope=s=1920x1080,format=yuv420p[v]" \
-map "[v]" -map 0:a avectorscope.mp4
Here avectorscope
is the specific representation type.
Here is a handy list of such formats: https://gist.github.com/seyoum/4455e9bed74241bfbd640a8083fd38b3 all of which are documented under: http://ffmpeg.org/ffmpeg-filters.html#Multimedia-Filters
See also:
Procedural image representation of audio
https://stackoverflow.com/questions/4468157/how-can-i-create-a-waveform-image-of-an-mp3-in-linux/63869342#63869342
Tested in Ubuntu 19.04, ffmpeg 4.1.3. The files uploaded fine to YouTube as of August 2019.