Coverage for pyguymer3/media/return_audio_bit_rate.py: 72%
18 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-08 18:47 +0000
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-08 18:47 +0000
1#!/usr/bin/env python3
3# Define function ...
4def return_audio_bit_rate(
5 fname,
6 /,
7 *,
8 cwd = None,
9 debug = __debug__,
10 ensureNFC = True,
11 ffprobePath = None,
12 playlist = -1,
13 timeout = 60.0,
14):
15 """
16 Return the bit rate of the first audio stream in the media file.
17 """
19 # Import standard modules ...
20 import shutil
22 # Import sub-functions ...
23 from .__ffprobe__ import __ffprobe__
24 from .ffprobe import ffprobe
26 # **************************************************************************
28 # Try to find the paths if the user did not provide them ...
29 if ffprobePath is None:
30 ffprobePath = shutil.which("ffprobe")
31 assert ffprobePath is not None, "\"ffprobe\" is not installed"
33 # **************************************************************************
35 # Make sure that this fname/playlist combination is in the global dictionary ...
36 if fname not in __ffprobe__:
37 __ffprobe__[fname] = {}
38 if playlist not in __ffprobe__[fname]:
39 if debug:
40 print(f"INFO: Running ffprobe(\"{fname}\", {playlist:d}) ...")
41 __ffprobe__[fname][playlist] = ffprobe(
42 fname,
43 cwd = cwd,
44 ensureNFC = ensureNFC,
45 ffprobePath = ffprobePath,
46 playlist = playlist,
47 timeout = timeout,
48 )
50 # Loop over streams ...
51 for stream in __ffprobe__[fname][playlist]["streams"]:
52 # Skip stream if it is not audio ...
53 if stream["codec_type"].strip().lower() != "audio":
54 continue
56 # Return bit rate ...
57 return int(stream["bit_rate"]) # [b/s]
59 # Return error ...
60 return -1