Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
92 lines (87 loc) · 2.69 KB
/
Copy pathutils.py
File metadata and controls
92 lines (87 loc) · 2.69 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from fractions import Fraction
from pathlib import Path
import uuid
from PIL import Image
import subprocess
import glob
def get_frames(path: Path, frames: list[int]) -> list[str]:
prefix = str(uuid.uuid4())
if path.is_dir():
for f in frames:
img: Image.Image = Image.open(path / f"{f+1:05d}.jpg")
# Resize for optimal token usage w/ gemini
img.thumbnail((768, 768), Image.Resampling.LANCZOS) # pyright: ignore
img.save(f"ffmpeg/{prefix}_frame{f:04d}.jpg")
else:
frame_select_str = "+".join([f"eq(n\\,{x})" for x in frames])
cmd = [
"ffmpeg",
"-nostdin",
"-i", str(path),
"-v", "error",
"-vf", f"select={frame_select_str},scale=768:768:force_original_aspect_ratio=decrease", # Resize for optimal token usage w/ gemini
"-vsync", "0",
"-q:v", "2", # High quality jpeg
f"ffmpeg/{prefix}_frame%04d.jpg"
]
subprocess.check_call(cmd)
paths = glob.glob(f"ffmpeg/{prefix}*.jpg")
return sorted(paths)
def get_frame_rate(path: Path) -> Fraction:
if path.is_dir():
return Fraction(3)
cmd = [
"ffprobe",
"-v",
"error",
"-select_streams",
"v:0",
"-show_entries",
"stream=r_frame_rate",
"-of",
"default=noprint_wrappers=1:nokey=1",
str(path),
]
try:
output = subprocess.check_output(cmd).decode("utf-8").strip()
return Fraction(output)
except Exception as e:
print(f"Error getting frame rate: {e}")
return Fraction(0)
def get_total_frames(path: Path) -> int:
if path.is_dir():
return len([f for f in path.iterdir() if f.is_file()])
else:
ffprobe_cmd = [
"ffprobe",
"-v",
"error",
"-count_frames",
"-select_streams",
"v:0",
"-show_entries",
"stream=nb_read_frames",
"-of",
"default=noprint_wrappers=1:nokey=1",
str(path),
]
try:
output = subprocess.check_output(ffprobe_cmd).decode("utf-8")
return int(output.strip())
except Exception:
print("ffprobe failed for file", path)
return 0
def get_duration(path: Path) -> float:
if path.is_dir():
return len([f for f in path.iterdir() if f.is_file()]) / 3.0
result = subprocess.run(
["ffprobe",
"-v", "error",
"-show_entries", "format=duration",
"-of", "default=noprint_wrappers=1:nokey=1",
str(path)
],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT
)
return float(result.stdout)