Tagged: Audacity

How to composite videos using FFMPEG

Video overlays are not easy but can be done.

I have two videos and I need to place them side-by-side in a new video. The dimensions first video (blob.mp4) was 1280×720. The second video (aoc.mp4) was 406×720.

The compositing was achieved by using a complex filter:

ffmpeg -i blob.mp4 -i aoc.mp4 -filter_complex "[0:v]pad=1686:720:0:[a]; [a][1:v]overlay=1280:0[b]" -map "[b]" -pix_fmt yuv420p aoc-meets-garbage-disposal.mp4

In the first part of the filter, I specified that the first input video (blob.mp4) will have a dimension of 1686×720, where 1686 is the total width of the both videos side-by-side. A map identifier “[a]” is used for this screen canvas. Next, with a semicolon delimiter, the second video is specified as appearing at 1280 pixels from the left edge at 0. (Yes, it begins at zero. I got a stupid “Overlay area … not within the main area … or zero-sized… Failed to configure input pad on Parsed_overlay_1” error when I thought I had to use 1281 as the starting point of the second video.) The overlaid video is then map identified as “[b]”. This map is then encoded to the output video.

Of course, this video does not have audio. So, I extracted the audio from both videos using FFMPEG, mixed them using Audacity (although I could have done it using FFMPEG as well), and slipstreamed the audio to the composite video using FFMPEG.

ffmpeg -i blob.mp4 -vn blob.mp3
ffmpeg -i aoc.mp4 -vn aoc.mp3

#Created garbage.mp3 from blob.mp3 and aoc.mp3 using Audacity

ffmpeg -i aoc-meets-garbage-disposal.mp4 -i garbage.mp3 -codec copy aoc-meets-garbage-disposal-mix.mp4

Alexandria Ocasio-Cortez, the US Congresswoman, lived in New York where garbage disposal machines were banned and people did not buy them even after the ban was lifted. She recently caused worldwide hilarity after posting an ominous video about her discovery of the garbage disposal machine in her sink. She must have at least seen them in movies or TV.


How to fix low-volume audio/video files with FFMPEG (for normalization) and/or Audacity (for Dynamic Range Compression)

Because my Internet connection is very cranky, I download online videos and play them offline on my WDTV media player. The WDTV remote does not have volume control. After I installed the wdlxtv open-source firmware, it has lost function of the Mute button. Now, there is no way to increase or decrease the volume without getting up from the chair.

Different videos have different volume levels. I wrote a Nautilus Action Configuration and a bash script to normalize audio and video files before I copied them to WDTV.

# Detect the max volume from this command
ffmpeg -y -i "$1/$2" -af "volumedetect" -an -vn -y -f null /dev/null

# Re-encode audio with this command after setting the DB level and output file name
ffmpeg -y -i "$1/$2" -af "volume=`echo $sDBLevel`dB" -c:a libmp3lame -b:a 128k "$sOutputFileName"

This worked fine for most files. Some video files continued to have low volume even though they were already normalized and couldn’t be normalized further. I then learned that this required the use of something called Dynamic Range Compression (DRC). DRC tries retains the waveform structure of the audio stream but compresses them against a threshold. Unfortunately, I compiled my FFMPEG binaries in 2013 and it did not seem to have filters for DRC. I cannot compile the latest FFMPEG source code because my Linux system is from 2010.

Instead, I choose the Dynamic Range Compressor option in Audacity. You load an audio file, select Compression from the Effects menu, adjust the settings and click OK to compress the audio and export it to a new MP3 file. This step couldn’t be automated.

I then used FFMPEG to replace the low-volume audio stream in the video with the compressed audio file generated by Audacity. In the FFMPEG command, I used to -i operators for two input files – the original video file and the new compressed audio file. I used the -map operator to copy the first stream of the first input file and the 3rd stream from the second input file to create a new video file. The second stream containing low-volume audio is dropped.

# Identify the streams in the input files
ffmpeg -i How_Low_Can_I_Go.mp4 -i "How_Low_Can_I_Go[COMPRESSED].mp3"

# Copy the video stream (0:0) without reencoding, drop 
# the second stream (0:1), use the third stream (1:0) for audio
ffmpeg -i How_Low_Can_I_Go.mp4 -i "How_Low_Can_I_Go[COMPRESSED].mp3" \
    -map 0:0 -map 1:0 \
    -vcodec copy -acodec copy \