LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-16-2016, 08:03 AM   #1
damn.snarky.bastard
Member
 
Registered: Aug 2015
Posts: 39

Rep: Reputation: Disabled
proactive solution for all your replay gain problems


Replay gain for movies.


They say there's no such thing as a program for replay gain for movies. They're probably right, I certainly won't argue. It just seems to me that the whole scenario is reactive anyway. What if there was a proactive solution? A way to calculate and apply the gain to the actual file, be it audio or video, before it is played?

I do it all the time with great success.

Just by chance one day the word "volumedetect" caught my eye. I know its not a proper word, but I was intrigued. If you read the very lengthy man pages for ffmpeg you will find two options. One is "volumedetect", the other is "volume="

ffmpeg -i foo -af volumedetect -f null /dev/null

will parse any audio file or the audio portion of a video file and calculate the gain required.{ Now, I haven't attempted to work with files with more than one audio track, but I assume (yes, I know what follows "assume", but I'm assuming a worst case scenario.) that you would have to specify which audio track you want to work with. All the usual ffmpeg rules should apply.} The gain required to adjust the volume level to 89db, arbitrarily referred to as "0db" will be output on your screen. Take that value and apply it to:

ffmpeg -y -i foo -af volume=the value from volumedetect /output directory/filename.

Sounds like a lot of work, especially if you have a large collection.

I am not a programmer, but I have a nifty little script. In fact, this was my very first bash script away from the "hello world" type. I had kludged together a line or two, but nothing serious. I will share a few scripts with you because the forums are filled with posts about replay gain, and I have a solution that is just a little different than what they are asking for. I doubt if my work is radically unique, but it works so well that since I'm giving it away for free, I'm offering a double your money refund if you don't like it.

The sed was given to me by one kind soul (I'm too lazy to try and find the thread to give proper credit, and ditto on the me being lazy to another who stuck by me for over a week while we figured out what to do and how to do it.

Before we get to the scripts, let me say a couple of things. These scripts only work on the volume levels. They will not recode your files to another format like mp4 or mkv. There's about a bazillion programs out there that will do a much better job at that than anything I could come up with. Besides, I still have nightmares from the time I did try to recode and adjust the volume at the same time. They are completely separate actions, but you can do them in whichever order you wish.

Also, this runs solely in batch mode - you try and adjust 1700+ files one by one! Whether one file or a whole bunch of them, just put in your working folder and fire it up. Try and not do your whole collection in one go because you really should let the script complete before starting a new step.

STEP 1

Here is a script that will parse a directory, create a new subdirectory called "sorted by volume" to place your files in and sort the multimedia files into folders by gain required. Confusing sentence, but run it and see.

Why sort them first? The target is a volume level of "0.0". If you take a file with a volume level of 0.0, and attempt to apply a gain to it you get a file with a volume level of -91.0 , so we need to sort them.

NOTE: My scripts may seem a little (a lot) paranoid but I got tired of bash "forgetting" to create my folders.



SORT FILES BY VOLUME

Code:
#!/bin/bash


# This script uses ffmpeg's audio filters -af volumedetect and -af volume=
# to adjust audio levels. See man ffmpeg.
#
# Adjust volume levels

# You do not have to convert your files to mkv, or any other format for that
# matter. This will adjust the volume levels of most formats. I don't know
# about avi files with packed b_frames. There are some out there, I just don't
# have any right now to test. In the past I have opened my avi files with 
# avidemux (should be in the # repositories of your distro.) There is a button 
# to the right of the "save" button that will show you information about your
# video, including whether it has packed b_frames or not. While you're in there
# anyway you may want to convert it to some # other format. Go right ahead,
# Avidemux is great.

hpwd=$PWD

until [ -d adjust\ volume ]; do
mkdir -p adjust\ volume ; if [ -d adjust\ volume ]; then break
else continue
fi
done

# Move all video files into the adjust\ volume folder. Since other types of
# files, say, srt, or jpg as just two examples, are not acted upon, they can
# not be left in the working directory as they will be counted in the total
# number of files and will end up causing an endless loop.

# The next line ensures that there will not be an error message should any of
# the following containers are not in the directory.

shopt -s nullglob nocaseglob
# you and add or subtract just about any file type that ffmpeg can handle
for filename in *.avi *.flv *.m4v *.mkv *.mov *.mp4 *.mpeg *.mpg *.wmv *.webm *ogg *flac *mp3 ; do
# move files to their own directory
mv "$filename" "$hpwd"/adjust\ volume
done

# cd to adjust\ volume as working directory
cd ./adjust\ volume

pwd=$PWD


# paranoid, but my folders ARE created
until [ -d original ]; do
mkdir -p original ; if [ -d original ]; then break
else continue
fi
done

until [ -d verify ]; do
mkdir -p verify ; if [ -d verify ]; then break
else continue
fi
done




# check to see if any files remaining in the folder. Make sure that you only
# have files with the following extensions in the folder. Any other type will
# set up an endless loop with no exit

# Yes, I know it is sloppy work repeating commands, but then this is my first
# serious bash script and still don't know how to call it in two different
# sections.

if [[ $(find . -maxdepth 0 -type f  | wc -l) -eq 0 ]]; then break; else continue; fi
shopt -s nullglob nocaseglob
for filename in *.avi *.flv *.m4v *.mkv *.mov *.mp4 *.mpeg *.mpg *.wmv *.webm *.flac *.mp3; do
ffmpeg -y -i "$filename" -af volumedetect -report -f null /dev/null
grep -E "max_volume" ffmpeg*.log
sed -nr '/max_volume:/ s/.*[[:space:]]-?([[:digit:]\.]+)[[:space:]]+dB$/\1/p' ffmpeg*.log > ffmpegnew.log
s=$(<ffmpegnew.log)
ffmpeg -y -i "$filename" -af volume="$s" "$pwd"/verify/"$filename"
mv "$filename" "$pwd"/original
rm -f ff*.log

done
This was the hardest part. Initially I called up bc to take care of some floating point math to separate the files that need work from those that didn't, but the results were erratic. Sometimes what worked before quit working altogether unless I rebooted. After two months of trying to move to the second stage where the audio is recoded with the gain applied I became disgusted and split it into two separate functions despite having to calculate the gain a second time.




STEP 2 Manually cd into "sorted by volume" cut and paste directory "0.0" (if it exists) into the top directory, then cd back. Any/All remaining folders will have values greater than 0.0

As we won't have to input any values, let's gather all of the files together.

Code:
 
find . -type f -exec mv {} ./ \;
Hopefully you've been around long enough to know this means find (recursively from this folder on down) any files and move them to this, the current working directory.

Let's get rid of the empty folders

Code:
find . -type d -empty -delete
Why specify directories only? Back when I was losing the battle with "bc" and floating point numbers I would sometimes end up with Zero byte movies. How can I tell if something is wrong if I delete the files before I see them?

STEP 3 The fun - or nap time depending on the number and nature of your files. Obviously small files like mp3s complete quicker than two hour movies.



Adjust volume levels

Code:
#!/bin/bash

# This script uses ffmpeg's audio filters -af volumedetect and -af volume=
# to adjust audio levels. See man ffmpeg.
#
# Adjust volume levels

# You do not have to convert your files to mkv, or any other format for that
# matter. This will adjust the volume levels of most formats. I don't know
# about avi files with packed b_frames. There are some out there, I just don't
# have any right now to test. In the past I have opened my avi files with
# avidemux (should be in the repositories of your distro.) There is a button to
# the right of the "save" button  that will show you information about your
# video, including whether it has packed b_frames or not. While you're in there
# anyway you may want to convert it to some other format. Go right ahead,
# Avidemux is great. 

# This may seem excessively paranoid, but I'm tired of bash "forgetting" to make
# the folders. The endless loops have only one escape - make the directory. Too
# often bash will skip over this and you could end up with garbage or data
# missing altogether.

# NOTE: It will be up to you to handle the sorting end of things. When this
# script has finished, cd into the folder "verify" and run the script "sort".
# Do not be surprized if some files have to be adjusted a couple of times. I
# have no idea why this is.


hpwd=$PWD

until [ -d adjust\ volume ]; do
mkdir -p adjust\ volume ; if [ -d adjust\ volume ]; then break
else continue
fi
done

# Move all video files into the adjust\ volume folder. Since other types of files, say, srt,  or jpg as just two examples, are not
# acted upon, they can not be left in the working
# directory as they will be counted and will end up causing an endless loop.

shopt -s nullglob nocaseglob
for filename in *.avi *.flv *.m4v *.mkv *.mov *.mp4 *.mpeg *.mpg *.wmv *.webm *ogg *flac *mp3 ; do
mv "$filename" "$hpwd"/adjust\ volume
done
# cd to adjust\ volume as working directory
cd ./adjust\ volume

pwd=$PWD

until [ -d original ]; do
mkdir -p original ; if [ -d original ]; then break
else continue
fi
done
until [ -d verify ]; do
mkdir -p verify ; if [ -d verify ]; then break
else continue
fi
done

# check to see if any files remaing in the folder. Make sure that you only have
# files with the following extensions in the folder. Any other type will set up
# an endless loop with no exit

if [[ $(find . -maxdepth 0 -type f  | wc -l) -eq 0 ]]; then break; else continue; fi
shopt -s nullglob nocaseglob
for filename in *.avi *.flv *.m4v *.mkv *.mov *.mp4 *.mpeg *.mpg *.wmv *.webm *.ogg *.flac *.mp3; do
ffmpeg -y -i "$filename" -af volumedetect -report -f null /dev/null
grep -E "max_volume" ffmpeg*.log
sed -nr '/max_volume:/ s/.*[[:space:]]-?([[:digit:]\.]+)[[:space:]]+dB$/\1/p' ffmpeg*.log > ffmpegnew.log
s=$(<ffmpegnew.log)
ffmpeg -y -i "$filename" -af volume="$s" "$pwd"/verify/"$filename"
mv "$filename" "$pwd"/original
rm -f ff*.log

done
STEP 4

cd into "adjust volume" (notice the folder name reflects the last action performed in case you forget). There will be two folders. I'm sure that "original" needs no explanation. I STRONGLY SUGGEST that you open "verify" and repeat STEP 2 and sort them by volume again as this does not always work 100 %. Although the gain level changes, and sometimes gets worse, you can repeat steps 2 and 3 as many times as necessary. The most I have had to do is adjust the volume three times. It happens on some files and not others, and I don't know why. I have taken a set of files, processed them, then repeated the procedure a number of times with consistent results. The files that had to be processed more than once on the first set of trials needed to be done again on subsequent runs.


TOTALLY OPTIONAL Just for s&@) and giggles one day I installed a file manager called "spacefm". What I like about it is that it is configurable from one end to the other. All of the scripts here as well as others are available to me from a right click menu. It takes only seconds to initialize a script without having to type in a bunch of stuff.

EXAMPLE: I had a usb hdd die on me from too many errors. There were so many errors that I decided to wipe the drive. The version of dd I was using did not include the new --progress option, but after 4 straight days it still had not completed. I have yet to find a program that can touch this drive and now I use it to prop up the short leg of my desk (not really). Now when I hotplug my other usb hdd I can simply right click and select my fsck script to unmount, run fsck, making corrections where needed, and remount the drive - automagically.

I am not affiliated or associated with spacefm in any way except as a guy in userland who heard about it and liked it.

If you find this useful, share it with friends. I suffer from depression and found myself so stressed that for two weeks I was either crashed out on my bed or pecking away at the keyboard and searching the web for answers. Don't let me have suffered for nothing, share!


PS If you can get the sort function with floating decimal point math working properly under bash - LET ME KNOW!

Last edited by damn.snarky.bastard; 05-17-2016 at 09:23 AM. Reason: tidy it up
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Replay Gain for Flac in Rhythmbox themeone Linux - General 1 05-17-2016 09:28 AM
hwm proactive mode required iftikharicp Linux - Newbie 13 04-02-2011 09:47 AM
Proactive versus reactive network monitoring CrystalBedell Linux - Networking 2 06-13-2010 12:24 AM
LXer: Freedom fellows get proactive LXer Syndicated Linux News 0 10-31-2006 01:03 PM
flac replay gain dukeinlondon Linux - Software 0 05-29-2006 01:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration