Ffmpeg audio visualization

As a matter of fact, this example mainly refers to -filter_complex (aka -lavfi), but I will also cover more general issues of using ffmpeg. Actually, I posted everything I needed on Github. So, what you need:

ON. Ffmpeg itself, imagemagick (make a transparent background and not only), git (if not). Debian (Ubuntu)

$ apt install -y ffmpeg imagemagick git

The actual mp3 file, better with metadata, the script can pull out the text and add it to the video.

A picture, in principle, for this example, any disc image.

Put mktrans… See README.md for instructions on how to create a transparent background. You can resize the image using convert from imagemagick or ffmpeg itself.

Put an example git clone https://github.com/zirf0/music_visual.git

Now let’s look inside

flr_show-freq-vol - текст фильтра

music_vision.sh - основной скрипт

valk500tr.png - картинка с диском (у меня 500x500 с прозрачным фоном)

wagner.gif - гифка к этой статье.

wagner_valkyries.mp3 - "Полет Валькирий" Вагнера

We start to disassemble the filter

" [0:a]showfreqs=s=600x240:mode=line:colors=red|blue,pad=1920:1080:0:x=1000:y=350[vf];

[0:a]showvolume=p=1,scale=1280:40[vv]; [1:v]scale=500:500,rotate=a=0.4*t/2:ow=iw/1:oh=ow[disk];

[vf][disk]overlay=(W-w)-1300:(H-h)/2,scale=1920:1080[l1];

[l1][vv]overlay=x=700:y=800,scale=1920:1080[l2]; [l2]drawtext=fontfile=NimbusRoman-BoldItalic.otf:textfile=metadata.txt: fontcolor=white:fontsize=50:box=1:boxcolor=black@0.5:boxborderw=5:x=(w-text_w)-500:y=(h-text_h)-800, format=yuv420p[fin] "

This is a text string, it is removed from the script for convenience. So the first line [0:a]…[vf] creates a video from an audio stream [vf] (video frequency – I named it that way to make it clearer). The next line works similarly, but the output of the showvolume filter is denoted as [vv]… Farther [1:v]…[disk] applies a rotate filter to our image (yes [1:v] video stream), the output is denoted as [disk]… Now we put on [vf] [disk], the pin is denoted as [l1] (layer 1). The next line works similarly, superimposing [vv] on [l1]… The last pair [l2] ..[fin] overlays the text that it takes from the metadata.txt file.

Let’s move on to the script, I will analyze only the significant lines, the rest should not cause difficulties.

data=$(ffprobe -i $mp3 2>&1 | egrep "artist|title") # get some data from mp3 metadada

#Uncomment len to get real duration

#len=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 $mp3 ) #duration of input, sec len=20 # for test only

Substrings from metadata get into the data variable, they are written to the metadata.txt file, which was mentioned above. len is the length of the mp3 in seconds, by default the main mechanism is commented out and the length is set to 20 seconds. By the way, the method for determining the duration is not entirely accurate in theory, but it works.

enc="-c:v h264 -crf 23 -preset medium -tune film -b:v 3M -c:a aac -b:a 192k" #executing string

exe=$(echo "ffmpeg -i $mp3 -loop 1 -i $disk -y -lavfi $filter -ss 00:00:00 -to $len $enc -shortest -map '[fin]' -map 0:a $out")

The enc variable contains the default values. For YouTube you can check here. In the exe variable, the ffmpeg command line, here is also [fin] from the filter. The key -loop 1 in front of the picture is important, it’s clear why. Streams must always be connected, that is, each output must go to an input. Why such a strange system – dumping a variable into a file and launching it? For historical reasons, it is easier to debug.

Now comes the most important thing. You can run ./music_vision.sh and get 20 seconds of video. But before creating a video, check out the following materials:

Magazine “Tester’s Life”, we need numbers for April 2014 and May 2016, there is a lot about ffmpeg in Russian and white.

https://github.com/SonicField/sonic-field-video… Lots of examples. For example, such a conclusion.

Many examples with video results.

Documentation ffmpeg

Google. Recommendations for YouTube videos.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *