Repository navigation
Expand file tree
/
Copy pathprocess_utils.py
More file actions
69 lines (57 loc) · 2.38 KB
/
Copy pathprocess_utils.py
File metadata and controls
69 lines (57 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""Resolve external tools explicitly and limit FFmpeg to media files."""
import os
from constants import FFMPEG_INPUT_FORMATS
def resolve_executable(command):
"""Return an absolute executable path without implicit working-directory search.
An explicit path is a user selection. Bare names are searched only in
absolute PATH entries outside the working directory. On Windows, accept
native .exe files only: .bat/.cmd arguments otherwise pass through cmd.exe
even with subprocess's default shell=False.
"""
if not command:
return None
command = os.fspath(command)
if os.name == "nt":
extension = os.path.splitext(command)[1].lower()
if not extension:
command += ".exe"
elif extension != ".exe":
return None
if os.path.dirname(command):
candidates = [os.path.abspath(command)]
else:
current_dir = os.path.normcase(os.path.realpath(os.getcwd()))
candidates = []
for directory in os.environ.get("PATH", "").split(os.pathsep):
directory = directory.strip('"')
if not os.path.isabs(directory):
continue
directory = os.path.realpath(directory)
if os.path.normcase(directory) == current_dir:
continue
candidates.append(os.path.join(directory, command))
for candidate in candidates:
candidate = os.path.realpath(candidate)
if os.name == "nt" and os.path.splitext(candidate)[1].lower() != ".exe":
continue
if os.path.isfile(candidate) and os.access(candidate, os.X_OK):
return candidate
return None
def require_executable(command):
resolved = resolve_executable(command)
if resolved is None:
raise FileNotFoundError(f"Executable not found in a trusted PATH directory: {command}")
return resolved
def local_media_input_args(filepath):
"""Build input arguments shared by probing, CUDA testing and conversion.
Absolute paths cannot become options or protocol URLs. Playlists and script
demuxers are deliberately excluded; this is not an OS-level decoder sandbox.
"""
filepath = os.path.abspath(filepath)
if not os.path.isfile(filepath):
raise FileNotFoundError(filepath)
return [
"-protocol_whitelist", "file",
"-format_whitelist", FFMPEG_INPUT_FORMATS,
"-i", filepath,
]