Download deploy/huggingface/ffmpeg_limited.py from new-arc/vector: direct link, hf CLI and curl.
- Browser
- Download file 3.21 kB
-
https://huggingface.co/spaces/new-arc/vector/resolve/main/deploy/huggingface/ffmpeg_limited.py
- Command line
-
hf download hf://spaces/new-arc/vector/deploy/huggingface/ffmpeg_limited.py
-
curl -L -o ffmpeg_limited.py https://huggingface.co/spaces/new-arc/vector/resolve/main/deploy/huggingface/ffmpeg_limited.py
3.21 kB
| #!/usr/local/bin/python | |
| """Space-only FFmpeg wrapper: serialize processes without changing codecs/quality.""" | |
| import errno | |
| import os | |
| from pathlib import Path | |
| import re | |
| import sys | |
| import time | |
| REAL_FFMPEG = "/usr/bin/ffmpeg" | |
| QUERIES = {"-version", "-h", "-help", "-formats", "-encoders", "-decoders", "-filters", "-hwaccels", "-codecs", "-buildconf", "-devices", "-protocols"} | |
| def limited_arguments(arguments, threads): | |
| if not 1 <= threads <= 2: | |
| raise ValueError("HF_FFMPEG_THREADS must be1 or2 on CPU Basic") | |
| if not arguments or any(argument in QUERIES for argument in arguments): | |
| return list(arguments) | |
| result = [] | |
| index = 0 | |
| while index < len(arguments): | |
| argument = arguments[index] | |
| if re.fullmatch(r"-(?:threads(?::[vas](?::\d+)?)?|filter_threads|filter_complex_threads)", argument): | |
| if index + 1 >= len(arguments): | |
| raise ValueError("Missing FFmpeg thread value") | |
| result.extend([argument, str(threads)]) | |
| index += 2 | |
| elif argument == "-i": | |
| # Decoder options reset for each input in a multi-range/crossfade job. | |
| result.extend(["-threads", str(threads), argument]) | |
| index += 1 | |
| else: | |
| result.append(argument) | |
| index += 1 | |
| prefix = ["-threads", str(threads), "-filter_threads", str(threads), "-filter_complex_threads", str(threads)] | |
| # SupoClip's FFmpeg commands have one final output target, including '-'. | |
| # The prefix caps decoding, and the output-scoped option caps encoding. | |
| if "-i" in result and result: | |
| result[-1:-1] = ["-threads", str(threads)] | |
| return prefix + result | |
| def main(): | |
| arguments = sys.argv[1:] | |
| command = [REAL_FFMPEG, *limited_arguments(arguments, int(os.environ.get("HF_FFMPEG_THREADS", "1")))] | |
| if not arguments or any(argument in QUERIES for argument in arguments): | |
| os.execv(REAL_FFMPEG, command) | |
| import fcntl | |
| directory = Path(os.environ.get("HF_RUNTIME_DIR", "/home/user/runtime")) | |
| directory.mkdir(parents=True, exist_ok=True) | |
| descriptor = os.open(directory / "ffmpeg.lock", os.O_CREAT | os.O_RDWR | getattr(os, "O_NOFOLLOW", 0), 0o600) | |
| timeout = float(os.environ.get("HF_FFMPEG_LOCK_TIMEOUT", "900")) | |
| if not 1 <= timeout <= 3600: | |
| raise ValueError("HF_FFMPEG_LOCK_TIMEOUT must be between1 and3600 seconds") | |
| deadline = time.monotonic() + timeout | |
| try: | |
| while True: | |
| try: | |
| fcntl.flock(descriptor, fcntl.LOCK_EX | fcntl.LOCK_NB) | |
| break | |
| except OSError as error: | |
| if error.errno not in (errno.EAGAIN, errno.EACCES): | |
| raise | |
| if time.monotonic() >= deadline: | |
| raise TimeoutError("Another media operation is busy; try again when it finishes") | |
| time.sleep(0.2) | |
| os.set_inheritable(descriptor, True) | |
| os.execv(REAL_FFMPEG, command) | |
| finally: | |
| os.close(descriptor) | |
| if __name__ == "__main__": | |
| try: | |
| main() | |
| except (OSError, ValueError, TimeoutError) as error: | |
| print(f"FFmpeg resource guard: {error}", file=sys.stderr) | |
| raise SystemExit(1) | |