Coverage for pyguymer3/media/return_video_format.py: 30%
57 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_video_format(
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 """Return the format of the first video stream in a media file
17 This function will return a pretty string of the format of the first video
18 stream in a media file.
20 Parameters
21 ----------
22 fname : str
23 the media file
24 cwd : str, optional
25 the directory to change to before running "ffprobe"
26 debug : bool, optional
27 print debug messages
28 ffprobePath : str, optional
29 the path to the "ffprobe" binary (if not provided then Python will
30 attempt to find the binary itself)
31 playlist : int, optional
32 for media files containing playlists, specify which playlist wants to be
33 surveyed
34 timeout : float, optional
35 the timeout for any requests/subprocess calls
37 Returns
38 -------
39 fmt : str
40 the format as a pretty string
42 Notes
43 -----
44 Copyright 2017 Thomas Guymer [1]_
46 References
47 ----------
48 .. [1] PyGuymer3, https://github.com/Guymer/PyGuymer3
49 """
51 # Import standard modules ...
52 import shutil
54 # Import sub-functions ...
55 from .__ffprobe__ import __ffprobe__
56 from .ffprobe import ffprobe
58 # **************************************************************************
60 # Try to find the paths if the user did not provide them ...
61 if ffprobePath is None:
62 ffprobePath = shutil.which("ffprobe")
63 assert ffprobePath is not None, "\"ffprobe\" is not installed"
65 # **************************************************************************
67 # Make sure that this fname/playlist combination is in the global dictionary ...
68 if fname not in __ffprobe__:
69 __ffprobe__[fname] = {}
70 if playlist not in __ffprobe__[fname]:
71 if debug:
72 print(f"INFO: Running ffprobe(\"{fname}\", {playlist:d}) ...")
73 __ffprobe__[fname][playlist] = ffprobe(
74 fname,
75 cwd = cwd,
76 ensureNFC = ensureNFC,
77 ffprobePath = ffprobePath,
78 playlist = playlist,
79 timeout = timeout,
80 )
82 # Loop over streams ...
83 for stream in __ffprobe__[fname][playlist]["streams"]:
84 # Skip stream if it is not video ...
85 if stream["codec_type"].strip().lower() != "video":
86 continue
88 # Return format ...
89 if "codec_name" in stream:
90 match stream["codec_name"]:
91 case "av1":
92 return "AV1"
93 case "flv1":
94 return "Sorenson Spark"
95 case "h264":
96 return "H.264"
97 case "mjpeg":
98 return "Motion JPEG"
99 case "mpeg4":
100 return "Xvid"
101 case "png":
102 return "PNG"
103 case "svq3":
104 return "Sorenson"
105 case "vp3":
106 return "VP3"
107 case "vp4":
108 return "VP4"
109 case "vp5":
110 return "VP5"
111 case "vp6":
112 return "VP6"
113 case "vp6a" | "vp6f":
114 return "VP6 Flash"
115 case "vp7":
116 return "VP7"
117 case "vp8":
118 return "VP8"
119 case "vp9":
120 return "VP9"
121 case "wmv1":
122 return "WMV 7"
123 case "wmv2":
124 return "WMV 8"
125 case "wmv3":
126 return "WMV 9"
127 case _:
128 raise ValueError(f'\"codec_name\" is an unexpected value ({repr(stream["codec_name"])})') from None
130 # Return error ...
131 return "ERROR"