Skip to content

Commit

Permalink
add functions to return the typo of audio/subtitle/video streams
Browse files Browse the repository at this point in the history
  • Loading branch information
Guymer committed Jul 19, 2024
1 parent 0767903 commit def1b84
Show file tree
Hide file tree
Showing 4 changed files with 248 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pyguymer3/media/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from .print_MP4_atoms import print_MP4_atoms
from .return_audio_bit_rate import return_audio_bit_rate
from .return_audio_channels import return_audio_channels
from .return_audio_format import return_audio_format
from .return_audio_sample_rate import return_audio_sample_rate
from .return_dict_of_bluray_playlists import return_dict_of_bluray_playlists
from .return_dict_of_ISO_audio_streams import return_dict_of_ISO_audio_streams
Expand All @@ -52,10 +53,12 @@
from .return_MP4_video_profile import return_MP4_video_profile
from .return_subtitle_bit_rate import return_subtitle_bit_rate
from .return_subtitle_extent import return_subtitle_extent
from .return_subtitle_format import return_subtitle_format
from .return_video_bit_depth import return_video_bit_depth
from .return_video_bit_rate import return_video_bit_rate
from .return_video_crop_parameters import return_video_crop_parameters
from .return_video_display_aspect_ratio import return_video_display_aspect_ratio
from .return_video_format import return_video_format
from .return_video_frame_rate import return_video_frame_rate
from .return_video_height import return_video_height
from .return_video_pixel_aspect_ratio import return_video_pixel_aspect_ratio
Expand Down
83 changes: 83 additions & 0 deletions pyguymer3/media/return_audio_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env python3

# Define function ...
def return_audio_format(fname, /, *, cwd = None, debug = False, playlist = -1, timeout = 60.0):
"""Return the format of the first audio stream in a media file
This function will return a pretty string of the format of the first audio
stream in a media file.
Parameters
----------
fname : str
the media file
cwd : str, optional
the directory to change to before running "ffprobe"
debug : bool, optional
print debug messages
playlist : int, optional
for media files containing playlists, specify which playlist wants to be
surveyed
timeout : float, optional
the timeout for any requests/subprocess calls
Returns
-------
fmt : str
the format as a pretty string
Notes
-----
Copyright 2017 Thomas Guymer [1]_
References
----------
.. [1] PyGuymer3, https://github.com/Guymer/PyGuymer3
"""

# Import sub-functions ...
from .__ffprobe__ import __ffprobe__
from .ffprobe import ffprobe

# Make sure that this fname/playlist combination is in the global dictionary ...
if fname not in __ffprobe__:
__ffprobe__[fname] = {}
if playlist not in __ffprobe__[fname]:
if debug:
print(f"INFO: Running ffprobe(\"{fname}\", {playlist:d}) ...")
__ffprobe__[fname][playlist] = ffprobe(fname, cwd = cwd, playlist = playlist, timeout = timeout)

# Loop over streams ...
for stream in __ffprobe__[fname][playlist]["streams"]:
# Skip stream if it is not audio ...
if stream["codec_type"].strip().lower() != "audio":
continue

# Return format ...
if "codec_name" in stream:
match stream["codec_name"]:
case "aac":
return "AAC"
case "adpcm_ima_qt":
return "Adaptive Differential PCM"
case "alac":
return "ALAC"
case "flac":
return "FLAC"
case "mp3":
return "MP3"
case "qdm2":
return "QDesign Music"
case "vorbis":
return "Vorbis"
case "wmapro":
return "WMA 9 Pro"
case "wmav1":
return "WMA 1"
case "wmav2":
return "WMA 2"
case _:
raise ValueError(f'\"codec_name\" is an unexpected value ({repr(stream["codec_name"])})') from None

# Return error ...
return "ERROR"
63 changes: 63 additions & 0 deletions pyguymer3/media/return_subtitle_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env python3

# Define function ...
def return_subtitle_format(fname, /, *, cwd = None, debug = False, playlist = -1, timeout = 60.0):
"""Return the format of the first subtitle stream in a media file
This function will return a pretty string of the format of the first
subtitle stream in a media file.
Parameters
----------
fname : str
the media file
cwd : str, optional
the directory to change to before running "ffprobe"
debug : bool, optional
print debug messages
playlist : int, optional
for media files containing playlists, specify which playlist wants to be
surveyed
timeout : float, optional
the timeout for any requests/subprocess calls
Returns
-------
fmt : str
the format as a pretty string
Notes
-----
Copyright 2017 Thomas Guymer [1]_
References
----------
.. [1] PyGuymer3, https://github.com/Guymer/PyGuymer3
"""

# Import sub-functions ...
from .__ffprobe__ import __ffprobe__
from .ffprobe import ffprobe

# Make sure that this fname/playlist combination is in the global dictionary ...
if fname not in __ffprobe__:
__ffprobe__[fname] = {}
if playlist not in __ffprobe__[fname]:
if debug:
print(f"INFO: Running ffprobe(\"{fname}\", {playlist:d}) ...")
__ffprobe__[fname][playlist] = ffprobe(fname, cwd = cwd, playlist = playlist, timeout = timeout)

# Loop over streams ...
for stream in __ffprobe__[fname][playlist]["streams"]:
# Skip stream if it is not subtitle ...
if stream["codec_type"].strip().lower() != "subtitle":
continue

# Return format ...
if "codec_name" in stream:
match stream["codec_name"]:
case _:
raise ValueError(f'\"codec_name\" is an unexpected value ({repr(stream["codec_name"])})') from None

# Return error ...
return "ERROR"
99 changes: 99 additions & 0 deletions pyguymer3/media/return_video_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/usr/bin/env python3

# Define function ...
def return_video_format(fname, /, *, cwd = None, debug = False, playlist = -1, timeout = 60.0):
"""Return the format of the first video stream in a media file
This function will return a pretty string of the format of the first video
stream in a media file.
Parameters
----------
fname : str
the media file
cwd : str, optional
the directory to change to before running "ffprobe"
debug : bool, optional
print debug messages
playlist : int, optional
for media files containing playlists, specify which playlist wants to be
surveyed
timeout : float, optional
the timeout for any requests/subprocess calls
Returns
-------
fmt : str
the format as a pretty string
Notes
-----
Copyright 2017 Thomas Guymer [1]_
References
----------
.. [1] PyGuymer3, https://github.com/Guymer/PyGuymer3
"""

# Import sub-functions ...
from .__ffprobe__ import __ffprobe__
from .ffprobe import ffprobe

# Make sure that this fname/playlist combination is in the global dictionary ...
if fname not in __ffprobe__:
__ffprobe__[fname] = {}
if playlist not in __ffprobe__[fname]:
if debug:
print(f"INFO: Running ffprobe(\"{fname}\", {playlist:d}) ...")
__ffprobe__[fname][playlist] = ffprobe(fname, cwd = cwd, playlist = playlist, timeout = timeout)

# Loop over streams ...
for stream in __ffprobe__[fname][playlist]["streams"]:
# Skip stream if it is not video ...
if stream["codec_type"].strip().lower() != "video":
continue

# Return format ...
if "codec_name" in stream:
match stream["codec_name"]:
case "av1":
return "AV1"
case "flv1":
return "Sorenson Spark"
case "h264":
return "H.264"
case "mjpeg":
return "Motion JPEG"
case "mpeg4":
return "Xvid"
case "png":
return "PNG"
case "svq3":
return "Sorenson"
case "vp3":
return "VP3"
case "vp4":
return "VP4"
case "vp5":
return "VP5"
case "vp6":
return "VP6"
case "vp6a" | "vp6f":
return "VP6 Flash"
case "vp7":
return "VP7"
case "vp8":
return "VP8"
case "vp9":
return "VP9"
case "wmv1":
return "WMV 7"
case "wmv2":
return "WMV 8"
case "wmv3":
return "WMV 9"
case _:
raise ValueError(f'\"codec_name\" is an unexpected value ({repr(stream["codec_name"])})') from None

# Return error ...
return "ERROR"

0 comments on commit def1b84

Please sign in to comment.