FFMPEG Loudnorm reading JSON data

Viewed 204

I tried to normalize some audio files using FFMPEG Loudnorm as described here.

However, in Python, I don't understand how to read data info from 1st pass.

My code:

getLoud =  subprocess.Popen(f"ffmpeg -i {file_path} -filter:a loudnorm=print_format=json -f null NULL", shell=True, stdout=subprocess.PIPE).stdout
getLoud =  getLoud.read().decode()
# parse json_str:
jsonstr_loud = json.loads(getLoud)

This gives me "errorMessage": "Expecting value: line 1 column 1 (char 0)"

I tried also this:

os.system(f"ffmpeg -i {file_path} -filter:a loudnorm=print_format=json -f null NULL")

and it outputs:

ffmpeg version N-60236-gffb000fff8-static https://johnvansickle.com/ffmpeg/  Copyright (c) 2000-2022 the FFmpeg developers...
...
[Parsed_loudnorm_0 @ 0x5921940] 
{
    "input_i" : "-9.33",
    "input_tp" : "-0.63",
    "input_lra" : "0.60",
    "input_thresh" : "-19.33",
    "output_i" : "-24.08",
    "output_tp" : "-15.40",
    "output_lra" : "0.60",
    "output_thresh" : "-34.08",
    "normalization_type" : "dynamic",
    "target_offset" : "0.08"
}

In Python, how can I use those parameters, such as input_i, input_tp etc. that I need for the 2nd pass?

I can't use ffmpeg-normalize because I'm using FFMPEG as a Layer in Lambda.

1 Answers

Kind of hacky, possibly flaky, but I did this and it has been plugging away at about 1GB an hour without a problem for at least 24 hours.

first_step_output = subprocess.run(
    "ffmpeg "
    f"-i \"{full_path}\" "
    "-af loudnorm=print_format=json "
    "-f null "
    "-",
    shell=True,
    stderr=subprocess.PIPE
)
if first_step_output.returncode == 0:
    output = json.loads(
        "{" + first_step_output.stderr.decode().split("{")[1]
    )
Related