-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add functions to return the typo of audio/subtitle/video streams
- Loading branch information
Showing
4 changed files
with
248 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |