Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.

 

Go Back   Doom9's Forum > General > Audio encoding

Reply
 
Thread Tools Search this Thread Display Modes
Old 7th December 2021, 04:54   #1  |  Link
zambelli
Doom9ing since 2001
 
zambelli's Avatar
 
Join Date: Oct 2001
Location: Seattle, WA, USA
Posts: 2,002
Conditional resampling with FFmpeg?

I'm writing a batch processing script for a workflow which requires all audio sources to be converted to 48 kHz stereo before transcoding, with one caveat: if the source sample rate is < 48 kHz, I don't want to upsample it to 48 kHz, I'd like to pass it through at its native sample rate.

So if the source sample rate is > 48000 Hz, I'd like the command line to be something like this:
Code:
ffmpeg -i "%sourcefile%" -af "aresample=resampler=soxr:precision=33:osr=48000:osf=flt:ocl=stereo:matrix_encoding=dplii" -acodec pcm_f32le -f wav
but if the source sample rate is <= 48000 Hz, I'd like the command line to be something like this:
Code:
ffmpeg -i "%sourcefile%" -af "aresample=resampler=soxr:precision=33:osf=flt:ocl=stereo:matrix_encoding=dplii" -acodec pcm_f32le -f wav
Is there a way to do this with just a single FFmpeg command line that conditionally inserts osr=48000 into the filter parameters based on the source sample rate? I think I'm looking for a solution analogous to using in_w and in_h constants in video scaler filter, but as far as I can tell there is no in_sample_rate equivalent I could use with the aresample filter.
zambelli is offline   Reply With Quote
Old 16th December 2021, 19:40   #2  |  Link
therube
Registered User
 
Join Date: Aug 2013
Posts: 191
Can't you check the sample rate ahead of time & branch in your script to the wanted ffmpeg line based on its' response?

ffprobe -loglevel error -select_streams a -show_entries stream=sample_rate -of default=noprint_wrappers=1:nokey=1 myfile.mp3

In my case, on my test, it returned "44100".

From there, depending on OS, or tools used, compare the returned result to your threshold (48000) & branch based on that.

pseudo code:
Code:
set answer = `ffmpeg...`
if %answer .lt. 48000 goto leaveasis: else goto osr48000:
therube is offline   Reply With Quote
Old 2nd January 2022, 02:29   #3  |  Link
Reino
Registered User
 
Reino's Avatar
 
Join Date: Nov 2005
Posts: 693
I don't know if this can be done with an FFmpeg command, but you can always do this with FFprobe in Batch:
Code:
FOR %A IN (*.wav) DO @(
  FOR /F %B IN ('ffprobe -v 0 -show_entries stream^=sample_rate -of default^=nk^=1:nw^=1 "%A"') DO @(
    IF %B GTR 48000 (
      ffmpeg -i "%A" -af "aresample=resampler=soxr:precision=33:osr=48000:osf=flt:ocl=stereo:matrix_encoding=dplii" -acodec pcm_f32le -f wav [...]
    ) ELSE (
      ffmpeg -i "%A" -af "aresample=resampler=soxr:precision=33:osf=flt:ocl=stereo:matrix_encoding=dplii" -acodec pcm_f32le -f wav [...]
    )
  )
)
__________________
My hobby website
Reino is offline   Reply With Quote
Old 3rd January 2022, 21:48   #4  |  Link
zambelli
Doom9ing since 2001
 
zambelli's Avatar
 
Join Date: Oct 2001
Location: Seattle, WA, USA
Posts: 2,002
I ended up going with exactly this kind of solution: use FFprobe to dump stream metadata, parse it, pass the right sample rate value to the FFmpeg command line.
It's a pity there's no easier way to do that, given how much flexibility is built into other FFmpeg filters like video scale.
zambelli is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

Forum Jump


All times are GMT +1. The time now is 07:36.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.