POV-Ray : Newsgroups : povray.off-topic : ffmpeg and normalizing audio volume Server Time
23 Dec 2024 11:27:50 EST (-0500)
  ffmpeg and normalizing audio volume (Message 1 to 10 of 10)  
From: Doctor John
Subject: ffmpeg and normalizing audio volume
Date: 7 May 2016 08:14:27
Message: <572ddc23$1@news.povray.org>
I've got approximately 40 GB of music files sitting on one of my boxes;
all under the top directory of ~/Music but then split into many other
sub-directories (as well as sub-sub-directories etc). Unfortunately,
since I got them from several sources, their relative volumes are all
over the place. If I play, for instance, Joan Osborne's 'One of Us' at a
reasonable volume and then follow it with The Stranglers' 'Always the
Sun', I get blown out of my chair by the sheer air pressure emanating
from my sub-woofer. I therefore need to normalize all my files - a mixed
bag of mp3s, oggs and flacs.
The slow way of doing it is to detect the maximum volume of each file
with 'ffmpeg -i example.mp3 -af "volumedetect" -f null /dev/null', note
the maximum volume (e.g. -10 dB) and then correct it with 'ffmpeg -i
example.mp3 -af "volume=10dB" example.mp3'.
Can anyone familiar with ffmpeg think of a way of scripting this so that
I don't have to manually re-encode every individual file?

BTW I'm using bash on a linux box.

John
-- 
Protect the Earth
It was not given to you by your parents
You hold it in trust for your children


Post a reply to this message

From: jr
Subject: Re: ffmpeg and normalizing audio volume
Date: 8 May 2016 06:00:08
Message: <572f0e28$1@news.povray.org>
hi,

On 07/05/2016 13:14, Doctor John wrote:
> ... I therefore need to normalize all my files - a mixed
> bag of mp3s, oggs and flacs.
> The slow way of doing it is to detect the maximum volume of each file
> with 'ffmpeg -i example.mp3 -af "volumedetect" -f null /dev/null', note

using ffmpeg v 0.8.7 here, get message "Unrecognised option 'af'".

> the maximum volume (e.g. -10 dB) and then correct it with 'ffmpeg -i
> example.mp3 -af "volume=10dB" example.mp3'.
> Can anyone familiar with ffmpeg think of a way of scripting this so that
> I don't have to manually re-encode every individual file?
> 
> BTW I'm using bash on a linux box.

I'm not familiar with 'ffmpeg' (I use 'lame' for recoding mp3s) and
don't know the exact format of the "volume" line you get from volumedetect.

however, I'd opt for a two-stage approach: (i) write a shell script
which processes a single file named on its command-line, (ii) use the
'find' utility to process all (named) files, eg, if your shell script is
~/mycmd:

  $ find ~/Music -type f -name \*.mp3 -exec ~/mycmd {} \;

the backslashes are necessary; find also supports a this-filename -or
that-filename syntax but requires backslach escapes all over the place.


jr.


Post a reply to this message

From: Doctor John
Subject: Re: ffmpeg and normalizing audio volume
Date: 8 May 2016 06:59:23
Message: <572f1c0b$1@news.povray.org>
On 08/05/16 11:00, jr wrote:
> hi,
> 
> On 07/05/2016 13:14, Doctor John wrote:
>> The slow way of doing it is to detect the maximum volume of each file
>> with 'ffmpeg -i example.mp3 -af "volumedetect" -f null /dev/null', note
> 
> using ffmpeg v 0.8.7 here, get message "Unrecognised option 'af'".
> 

Time to update, methinks. I'm using version 3.0.1

> 
> I'm not familiar with 'ffmpeg' (I use 'lame' for recoding mp3s) and
> don't know the exact format of the "volume" line you get from volumedetect.
> 

Last half of console stream, the line you want is the penultimate one:

Output #0, null, to '/dev/null':
  Metadata:
    encoder         : Lavf57.25.100
    Stream #0:0: Audio: pcm_s16le, 44100 Hz, stereo, s16, 1411 kb/s
    Metadata:
      TITLE           : Always The Sun
      ARTIST          : The Stranglers
      ALBUM           : Unknown Disc
      encoder         : Lavc57.24.102 pcm_s16le
Stream mapping:
  Stream #0:0 -> #0:0 (vorbis (native) -> pcm_s16le (native))
Press [q] to stop, [?] for help
size=N/A time=00:04:05.16 bitrate=N/A speed= 371x
video:0kB audio:42234kB subtitle:0kB other streams:0kB global
headers:0kB muxing overhead: unknown
[Parsed_volumedetect_0 @ 0x1fb3580] n_samples: 21623936
[Parsed_volumedetect_0 @ 0x1fb3580] mean_volume: -13.1 dB
[Parsed_volumedetect_0 @ 0x1fb3580] max_volume: 0.0 dB
[Parsed_volumedetect_0 @ 0x1fb3580] histogram_0db: 30166

> however, I'd opt for a two-stage approach: (i) write a shell script
> which processes a single file named on its command-line, (ii) use the
> 'find' utility to process all (named) files, eg, if your shell script is
> ~/mycmd:
> 
>   $ find ~/Music -type f -name \*.mp3 -exec ~/mycmd {} \;
> 
> the backslashes are necessary; find also supports a this-filename -or
> that-filename syntax but requires backslach escapes all over the place.
> 

So, as I understand it and assuming I want to process all mp3s, oggs and
flacs together, I need to 'find' each music file then use 'grep' to
strip out the max_volume numbers, invert those and put the resulting
number into  'ffmpeg -i [filename] -af "volume=[whatever]dB" [filename]'.

Of course my real problem is that I usually bugger up any script
requiring the use of regexes.

Thanks for taking the time to think about the process

John

-- 
Protect the Earth
It was not given to you by your parents
You hold it in trust for your children


Post a reply to this message

From: Doctor John
Subject: Re: ffmpeg and normalizing audio volume
Date: 8 May 2016 08:03:21
Message: <572f2b09$1@news.povray.org>
On 08/05/16 11:59, Doctor John wrote:
> Of course my real problem is that I usually bugger up any script
> requiring the use of regexes.
> 
> Thanks for taking the time to think about the process
> 
> John
> 

Just came across this script. A bit of thought might produce something I
can work with.

John
-- 
Protect the Earth
It was not given to you by your parents
You hold it in trust for your children


Post a reply to this message


Attachments:
Download 'ffmpeg_normalize.sh.txt' (3 KB)

From: jr
Subject: Re: ffmpeg and normalizing audio volume
Date: 8 May 2016 08:24:31
Message: <572f2fff@news.povray.org>
hi,

On 08/05/2016 11:59, Doctor John wrote:
> Last half of console stream, the line you want is the penultimate one:
> [Parsed_volumedetect_0 @ 0x1fb3580] n_samples: 21623936
> [Parsed_volumedetect_0 @ 0x1fb3580] mean_volume: -13.1 dB
> [Parsed_volumedetect_0 @ 0x1fb3580] max_volume: 0.0 dB
> [Parsed_volumedetect_0 @ 0x1fb3580] histogram_0db: 30166
> 

off the top of my head, ie untested, write a script containing the
following:

#!/bin/sh

DBV=$(ffmpeg -i "$1" -af "volumedetect" -f null /dev/null | \
       grep max_volume | cut -d: -f2 | cut -d' ' -f2)
#                                               ^^^
# not sure what the /dev/null does without '>' or '&>' redirection.
# typo?
# DBV will now have 0.0.

ffmpeg -i "$1" -af "volume=${DBV}dB" "$1"



then make it executable, ie $ chmod u+x mycmd

if the ffmpeg commands work exactly the same for all three file types,
run 'find' either three times with different extensions, or wrestle with
'find' and supply all three linked with '-or's.

>>   $ find ~/Music -type f -name \*.mp3 -exec ~/mycmd {} \;

> So, as I understand it and assuming I want to process all mp3s, oggs and
> flacs together, I need to 'find' each music file then use 'grep' to

find does the finding and recursive tree walk (provided all directories
below ~/Music reside on the same filesystem).

> Of course my real problem is that I usually bugger up any script
> requiring the use of regexes.

you're not alone.  :-)

jr.


Post a reply to this message

From: Doctor John
Subject: Re: ffmpeg and normalizing audio volume
Date: 8 May 2016 08:44:02
Message: <572f3492$1@news.povray.org>
On 08/05/16 13:24, jr wrote:
> hi,
> 
> On 08/05/2016 11:59, Doctor John wrote:
>> Last half of console stream, the line you want is the penultimate one:
>> [Parsed_volumedetect_0 @ 0x1fb3580] n_samples: 21623936
>> [Parsed_volumedetect_0 @ 0x1fb3580] mean_volume: -13.1 dB
>> [Parsed_volumedetect_0 @ 0x1fb3580] max_volume: 0.0 dB
>> [Parsed_volumedetect_0 @ 0x1fb3580] histogram_0db: 30166
>>
> 
> off the top of my head, ie untested, write a script containing the
> following:
> 
> #!/bin/sh
> 
> DBV=$(ffmpeg -i "$1" -af "volumedetect" -f null /dev/null | \
>        grep max_volume | cut -d: -f2 | cut -d' ' -f2)
> #                                               ^^^
> # not sure what the /dev/null does without '>' or '&>' redirection.
> # typo?
> # DBV will now have 0.0.
> 
> ffmpeg -i "$1" -af "volume=${DBV}dB" "$1"
> 
> 
> 
> then make it executable, ie $ chmod u+x mycmd
> 
> if the ffmpeg commands work exactly the same for all three file types,
> run 'find' either three times with different extensions, or wrestle with
> 'find' and supply all three linked with '-or's.
> 
>>>   $ find ~/Music -type f -name \*.mp3 -exec ~/mycmd {} \;
> 
>> So, as I understand it and assuming I want to process all mp3s, oggs and
>> flacs together, I need to 'find' each music file then use 'grep' to
> 
> find does the finding and recursive tree walk (provided all directories
> below ~/Music reside on the same filesystem).
> 
>> Of course my real problem is that I usually bugger up any script
>> requiring the use of regexes.
> 
> you're not alone.  :-)
> 
> jr.
> 

I've just been informed by She_who_must_be_obeyed that if I don't lay
the table for Sunday lunch, the consequences will be terrible.

I'll do some testing later on this evening

John

-- 
Protect the Earth
It was not given to you by your parents
You hold it in trust for your children


Post a reply to this message

From: jr
Subject: Re: ffmpeg and normalizing audio volume
Date: 8 May 2016 08:56:55
Message: <572f3797$1@news.povray.org>
On 08/05/2016 13:24, jr wrote:

> find does the finding and recursive tree walk (provided all directories
> below ~/Music reside on the same filesystem).

correction: find can be limited ('-xdev' option) to a single filesystem.


Post a reply to this message

From: jr
Subject: Re: ffmpeg and normalizing audio volume
Date: 8 May 2016 11:11:49
Message: <572f5735$1@news.povray.org>
hi,

On 08/05/2016 13:44, Doctor John wrote:
> I've just been informed by She_who_must_be_obeyed that if I don't lay
> the table for Sunday lunch, the consequences will be terrible.

:-)

> I'll do some testing later on this evening

I'm thinking with 40G data (3500+? files) even a modest improvement to
the suggested script will be worthwhile.  the pipeline getting the dB
value uses four processes, using 'sed' instead of grep etc
cuts that to two:

DBV=$(ffmpeg -i "$1" -af volumedetect -f null /dev/null | sed -r -n \
-e '/max_volume:/s,^[^:]+: +[+-]?([[:digit:]]+\.[[:digit:]]+) dB,\1,p')

(the RE is copied and pasted so ought to be correct.  if you need the
sign, simply shift the '(' 5 characters to the left)

btw, there's a widely available PDF of the "sed & awk" book, it's a good
resource to have to hand when working with patterns and REs.

jr.


Post a reply to this message

From: Doctor John
Subject: Re: ffmpeg and normalizing audio volume
Date: 13 May 2016 05:22:24
Message: <57359cd0$1@news.povray.org>
On 08/05/16 13:56, jr wrote:
> On 08/05/2016 13:24, jr wrote:
> 
>> find does the finding and recursive tree walk (provided all directories
>> below ~/Music reside on the same filesystem).
> 
> correction: find can be limited ('-xdev' option) to a single filesystem.
> 

Apologies for late reply. Real Life intervened.

I think I've cracked the find command:
find -type f -regex ".*/.*\.\(mp3\|ogg\|flac\)"

It gives the result I want; now all I need to do is put the whole script
together and I'm winning.

Unfortunately, I won't have the time till Saturday/Sunday but I am
optimistic that I'm on the home straight.

John
-- 
Protect the Earth
It was not given to you by your parents
You hold it in trust for your children


Post a reply to this message

From: jr
Subject: Re: ffmpeg and normalizing audio volume
Date: 13 May 2016 17:43:32
Message: <57364a84$1@news.povray.org>
On 13/05/2016 10:22, Doctor John wrote:
> I think I've cracked the find command:
> find -type f -regex ".*/.*\.\(mp3\|ogg\|flac\)"

natty.  I had not "registered" this option before.

> It gives the result I want; now all I need to do is put the whole script
> together and I'm winning.

it's one of the things I love about UNIX(like) environments, so many
ways to skin the proverbial cat.

> Unfortunately, I won't have the time till Saturday/Sunday but I am
> optimistic that I'm on the home straight.

you will try on a few sample files first, I assume?
(from bitter experience and all that :-))

jr.


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.