Commit ·
a96d4f6
1
Parent(s): 5ea61d9
utilities
Browse files- compressVideos.py +66 -0
- copyKeepFrames.py +42 -0
- copyKeepFramesProcessed.py +45 -0
- cut.py +54 -0
- encode.py +66 -0
- generateDuplicateMeta.py +79 -0
- toImageHQ.py +34 -0
compressVideos.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import subprocess
|
| 3 |
+
import re
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
def get_video_dimensions(file_path: str) -> tuple[int, int] | None:
|
| 7 |
+
"""Extract original video width and height from HandBrakeCLI --scan output."""
|
| 8 |
+
try:
|
| 9 |
+
# Run HandBrakeCLI --scan and capture stderr (it prints to stderr)
|
| 10 |
+
result = subprocess.run(
|
| 11 |
+
["HandBrakeCLI", "-i", file_path, "--scan"],
|
| 12 |
+
stderr=subprocess.PIPE,
|
| 13 |
+
stdout=subprocess.PIPE,
|
| 14 |
+
text=True
|
| 15 |
+
)
|
| 16 |
+
output = result.stderr + result.stdout
|
| 17 |
+
|
| 18 |
+
# Find "+ size: 1920x1080" line
|
| 19 |
+
match = re.search(r"\+ size:\s*(\d+)x(\d+)", output)
|
| 20 |
+
if match:
|
| 21 |
+
width = int(match.group(1))
|
| 22 |
+
height = int(match.group(2))
|
| 23 |
+
return width, height
|
| 24 |
+
else:
|
| 25 |
+
raise RuntimeError("Could not parse width and height from HandBrakeCLI output.")
|
| 26 |
+
except Exception as e:
|
| 27 |
+
print(f"[ERROR] Failed to get dimensions for {file_path}: {e}")
|
| 28 |
+
|
| 29 |
+
def process_video(file_path: str, qualities=range(40, 11, -2)):
|
| 30 |
+
"""Process a video at multiple quality levels, scaling to 0.25x width."""
|
| 31 |
+
width,height = get_video_dimensions(file_path)
|
| 32 |
+
if width is None or height is None:
|
| 33 |
+
return
|
| 34 |
+
|
| 35 |
+
new_width = width // 4
|
| 36 |
+
new_height = height // 4
|
| 37 |
+
print(f"Original width = {width}, new width = {new_width}")
|
| 38 |
+
print(f"Original height = {height}, new width = {new_height}")
|
| 39 |
+
|
| 40 |
+
# Ensure output folder exists
|
| 41 |
+
out_dir = Path("processed")
|
| 42 |
+
out_dir.mkdir(exist_ok=True)
|
| 43 |
+
|
| 44 |
+
base_name = Path(file_path).stem
|
| 45 |
+
for q in qualities:
|
| 46 |
+
out_file = out_dir / f"{base_name}_{q}.mp4"
|
| 47 |
+
print(f"Encoding {file_path} → {out_file} (quality={q})")
|
| 48 |
+
|
| 49 |
+
cmd = [
|
| 50 |
+
"HandBrakeCLI",
|
| 51 |
+
"-i", file_path,
|
| 52 |
+
"-o", str(out_file),
|
| 53 |
+
"-q", str(q),
|
| 54 |
+
"--width", str(new_width),
|
| 55 |
+
"--height", str(new_height)
|
| 56 |
+
]
|
| 57 |
+
subprocess.run(cmd, check=True)
|
| 58 |
+
|
| 59 |
+
def main():
|
| 60 |
+
# Process all mp4 files in the current directory
|
| 61 |
+
for file in Path(".").glob("*.mp4"):
|
| 62 |
+
print(f"\n=== Processing {file} ===")
|
| 63 |
+
process_video(str(file))
|
| 64 |
+
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
main()
|
copyKeepFrames.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import shutil
|
| 4 |
+
|
| 5 |
+
base_folder = "hq_og"
|
| 6 |
+
output_base = os.path.join("dedup", base_folder)
|
| 7 |
+
|
| 8 |
+
# Make sure dedup/hq_og exists
|
| 9 |
+
os.makedirs(output_base, exist_ok=True)
|
| 10 |
+
|
| 11 |
+
# Iterate over JSON filter files in hq_og
|
| 12 |
+
for file in os.listdir(base_folder):
|
| 13 |
+
if not file.endswith("_filter.json"):
|
| 14 |
+
continue
|
| 15 |
+
|
| 16 |
+
json_path = os.path.join(base_folder, file)
|
| 17 |
+
|
| 18 |
+
with open(json_path, "r") as f:
|
| 19 |
+
data = json.load(f) # this is your list of keep_files
|
| 20 |
+
moviename = data["moviename"]
|
| 21 |
+
keep_files = data["keep_files"]
|
| 22 |
+
|
| 23 |
+
# Extract folder name (before "_filter.json")
|
| 24 |
+
folder_name = file.replace("_filter.json", "")
|
| 25 |
+
input_folder = os.path.join(base_folder, folder_name)
|
| 26 |
+
output_folder = os.path.join(output_base, folder_name)
|
| 27 |
+
|
| 28 |
+
os.makedirs(output_folder, exist_ok=True)
|
| 29 |
+
|
| 30 |
+
print(f"Processing {folder_name}: copying {len(keep_files)} files")
|
| 31 |
+
|
| 32 |
+
for img_name in keep_files:
|
| 33 |
+
src = os.path.join(input_folder, img_name)
|
| 34 |
+
dst_name = f"{folder_name}_{img_name}"
|
| 35 |
+
dst = os.path.join(output_folder, dst_name)
|
| 36 |
+
|
| 37 |
+
if os.path.exists(src):
|
| 38 |
+
shutil.copy2(src, dst)
|
| 39 |
+
else:
|
| 40 |
+
print(f"Warning: file not found {src}")
|
| 41 |
+
|
| 42 |
+
print("Copying complete. Deduplicated dataset saved in /dedup/hq_og/")
|
copyKeepFramesProcessed.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import shutil
|
| 4 |
+
|
| 5 |
+
base_processed_folder = "processed/lq"
|
| 6 |
+
base_folder = "hq_og"
|
| 7 |
+
processed_folder = "lq"
|
| 8 |
+
output_base = os.path.join("dedup", processed_folder)
|
| 9 |
+
|
| 10 |
+
# Make sure dedup/hq_og exists
|
| 11 |
+
os.makedirs(output_base, exist_ok=True)
|
| 12 |
+
|
| 13 |
+
# Iterate over JSON filter files in hq_og
|
| 14 |
+
for file in os.listdir(base_folder):
|
| 15 |
+
if not file.endswith("_filter.json"):
|
| 16 |
+
continue
|
| 17 |
+
|
| 18 |
+
json_path = os.path.join(base_folder, file)
|
| 19 |
+
|
| 20 |
+
with open(json_path, "r") as f:
|
| 21 |
+
data = json.load(f) # this is your list of keep_files
|
| 22 |
+
moviename = data["moviename"]
|
| 23 |
+
keep_files = data["keep_files"]
|
| 24 |
+
qualities = [12,14,16,18,20,22,24,26,28,30,32,34,36,38,40]
|
| 25 |
+
for quality in qualities:
|
| 26 |
+
folder_name = moviename + f"_{quality}"
|
| 27 |
+
|
| 28 |
+
input_folder = os.path.join(base_processed_folder, folder_name)
|
| 29 |
+
output_folder = os.path.join(output_base, folder_name)
|
| 30 |
+
|
| 31 |
+
os.makedirs(output_folder, exist_ok=True)
|
| 32 |
+
|
| 33 |
+
print(f"Processing {folder_name}: copying {len(keep_files)} files")
|
| 34 |
+
|
| 35 |
+
for img_name in keep_files:
|
| 36 |
+
src = os.path.join(input_folder, img_name)
|
| 37 |
+
dst_name = f"{folder_name}_{img_name}"
|
| 38 |
+
dst = os.path.join(output_folder, dst_name)
|
| 39 |
+
|
| 40 |
+
if os.path.exists(src):
|
| 41 |
+
shutil.copy2(src, dst)
|
| 42 |
+
else:
|
| 43 |
+
print(f"Warning: file not found {src}")
|
| 44 |
+
|
| 45 |
+
print("Copying complete. Deduplicated dataset saved in /dedup/hq_og/")
|
cut.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import subprocess
|
| 2 |
+
import sys
|
| 3 |
+
import shlex
|
| 4 |
+
|
| 5 |
+
def get_duration(input_file):
|
| 6 |
+
"""Get video duration (in seconds) using ffprobe."""
|
| 7 |
+
result = subprocess.run(
|
| 8 |
+
[
|
| 9 |
+
"ffprobe",
|
| 10 |
+
"-v", "error",
|
| 11 |
+
"-show_entries", "format=duration",
|
| 12 |
+
"-of", "default=noprint_wrappers=1:nokey=1",
|
| 13 |
+
input_file,
|
| 14 |
+
],
|
| 15 |
+
stdout=subprocess.PIPE,
|
| 16 |
+
stderr=subprocess.PIPE,
|
| 17 |
+
text=True
|
| 18 |
+
)
|
| 19 |
+
return float(result.stdout.strip())
|
| 20 |
+
|
| 21 |
+
def cut_video(input_file, output_file, skip_start, skip_end):
|
| 22 |
+
"""Cut video: skip first skip_start seconds, drop last skip_end seconds."""
|
| 23 |
+
duration = get_duration(input_file)
|
| 24 |
+
if duration <= (skip_start + skip_end):
|
| 25 |
+
raise ValueError("Video too short for given cut parameters.")
|
| 26 |
+
|
| 27 |
+
# Compute new duration
|
| 28 |
+
new_duration = duration - skip_start - skip_end
|
| 29 |
+
|
| 30 |
+
# Run ffmpeg with stream copy
|
| 31 |
+
cmd = [
|
| 32 |
+
"ffmpeg",
|
| 33 |
+
"-y", # overwrite output
|
| 34 |
+
"-ss", str(skip_start),
|
| 35 |
+
"-i", input_file,
|
| 36 |
+
"-t", str(new_duration),
|
| 37 |
+
"-c", "copy",
|
| 38 |
+
output_file
|
| 39 |
+
]
|
| 40 |
+
print("Running:", " ".join(shlex.quote(c) for c in cmd))
|
| 41 |
+
subprocess.run(cmd, check=True)
|
| 42 |
+
print(f"Trimmed video saved to {output_file}")
|
| 43 |
+
|
| 44 |
+
if __name__ == "__main__":
|
| 45 |
+
if len(sys.argv) != 5:
|
| 46 |
+
print("Usage: python cut_video.py input.mp4 output.mp4 skip_start_seconds skip_end_seconds")
|
| 47 |
+
sys.exit(1)
|
| 48 |
+
|
| 49 |
+
input_file = sys.argv[1]
|
| 50 |
+
output_file = sys.argv[2]
|
| 51 |
+
skip_start = int(sys.argv[3])
|
| 52 |
+
skip_end = int(sys.argv[4])
|
| 53 |
+
|
| 54 |
+
cut_video(input_file, output_file, skip_start, skip_end)
|
encode.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import subprocess
|
| 3 |
+
import re
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
def get_video_dimensions(file_path: str) -> tuple[int, int] | None:
|
| 7 |
+
"""Extract original video width and height from HandBrakeCLI --scan output."""
|
| 8 |
+
try:
|
| 9 |
+
# Run HandBrakeCLI --scan and capture stderr (it prints to stderr)
|
| 10 |
+
result = subprocess.run(
|
| 11 |
+
["HandBrakeCLI", "-i", file_path, "--scan"],
|
| 12 |
+
stderr=subprocess.PIPE,
|
| 13 |
+
stdout=subprocess.PIPE,
|
| 14 |
+
text=True
|
| 15 |
+
)
|
| 16 |
+
output = result.stderr + result.stdout
|
| 17 |
+
|
| 18 |
+
# Find "+ size: 1920x1080" line
|
| 19 |
+
match = re.search(r"\+ size:\s*(\d+)x(\d+)", output)
|
| 20 |
+
if match:
|
| 21 |
+
width = int(match.group(1))
|
| 22 |
+
height = int(match.group(2))
|
| 23 |
+
return width, height
|
| 24 |
+
else:
|
| 25 |
+
raise RuntimeError("Could not parse width and height from HandBrakeCLI output.")
|
| 26 |
+
except Exception as e:
|
| 27 |
+
print(f"[ERROR] Failed to get dimensions for {file_path}: {e}")
|
| 28 |
+
|
| 29 |
+
def process_video(file_path: str, qualities=range(40, 11, -2)):
|
| 30 |
+
"""Process a video at multiple quality levels, scaling to 0.25x width."""
|
| 31 |
+
width,height = get_video_dimensions(file_path)
|
| 32 |
+
if width is None or height is None:
|
| 33 |
+
return
|
| 34 |
+
|
| 35 |
+
new_width = width // 4
|
| 36 |
+
new_height = height // 4
|
| 37 |
+
print(f"Original width = {width}, new width = {new_width}")
|
| 38 |
+
print(f"Original height = {height}, new width = {new_height}")
|
| 39 |
+
|
| 40 |
+
# Ensure output folder exists
|
| 41 |
+
out_dir = Path("processed")
|
| 42 |
+
out_dir.mkdir(exist_ok=True)
|
| 43 |
+
|
| 44 |
+
base_name = Path(file_path).stem
|
| 45 |
+
for q in qualities:
|
| 46 |
+
out_file = out_dir / f"{base_name}_{q}.mp4"
|
| 47 |
+
print(f"Encoding {file_path} → {out_file} (quality={q})")
|
| 48 |
+
|
| 49 |
+
cmd = [
|
| 50 |
+
"HandBrakeCLI",
|
| 51 |
+
"-i", file_path,
|
| 52 |
+
"-o", str(out_file),
|
| 53 |
+
"-q", str(q),
|
| 54 |
+
"--width", str(new_width),
|
| 55 |
+
"--height", str(new_height)
|
| 56 |
+
]
|
| 57 |
+
subprocess.run(cmd, check=True)
|
| 58 |
+
|
| 59 |
+
def main():
|
| 60 |
+
# Process all mp4 files in the current directory
|
| 61 |
+
for file in Path(".").glob("*.mp4"):
|
| 62 |
+
print(f"\n=== Processing {file} ===")
|
| 63 |
+
process_video(str(file))
|
| 64 |
+
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
main()
|
generateDuplicateMeta.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import imagehash
|
| 5 |
+
|
| 6 |
+
base_folder = "hq_og"
|
| 7 |
+
HASH_SIZE = 16
|
| 8 |
+
SIMILARITY_THRESHOLD = HASH_SIZE*HASH_SIZE * 0.2
|
| 9 |
+
|
| 10 |
+
# Function to compute average hash of an image
|
| 11 |
+
def get_image_hash(image_path):
|
| 12 |
+
try:
|
| 13 |
+
with Image.open(image_path) as img:
|
| 14 |
+
return imagehash.average_hash(img, hash_size=HASH_SIZE)
|
| 15 |
+
except Exception as e:
|
| 16 |
+
print(f"Error hashing {image_path}: {e}")
|
| 17 |
+
return None
|
| 18 |
+
|
| 19 |
+
# Iterate over all subfolders in hq_og
|
| 20 |
+
for folder in os.listdir(base_folder):
|
| 21 |
+
index=0
|
| 22 |
+
folder_path = os.path.join(base_folder, folder)
|
| 23 |
+
if not os.path.isdir(folder_path):
|
| 24 |
+
continue
|
| 25 |
+
|
| 26 |
+
print(f"Processing folder: {folder_path}")
|
| 27 |
+
|
| 28 |
+
hashes = {}
|
| 29 |
+
removed_files = []
|
| 30 |
+
keep_files = []
|
| 31 |
+
keep_files2 = []
|
| 32 |
+
|
| 33 |
+
for filename in os.listdir(folder_path):
|
| 34 |
+
file_path = os.path.join(folder_path, filename)
|
| 35 |
+
if not os.path.isfile(file_path):
|
| 36 |
+
continue
|
| 37 |
+
index+=1
|
| 38 |
+
if index%100==0:
|
| 39 |
+
print(f"At {index} image")
|
| 40 |
+
print("Discard Rate:", len(removed_files)/index)
|
| 41 |
+
|
| 42 |
+
file_hash = get_image_hash(file_path)
|
| 43 |
+
if file_hash is None:
|
| 44 |
+
continue
|
| 45 |
+
|
| 46 |
+
# Check against already kept images
|
| 47 |
+
is_duplicate = False
|
| 48 |
+
for h, kept_file in keep_files:
|
| 49 |
+
if file_hash - h <= SIMILARITY_THRESHOLD:
|
| 50 |
+
# This image is too similar to one we already kept
|
| 51 |
+
#os.remove(file_path)
|
| 52 |
+
removed_files.append(filename)
|
| 53 |
+
is_duplicate = True
|
| 54 |
+
break
|
| 55 |
+
|
| 56 |
+
if not is_duplicate:
|
| 57 |
+
keep_files.append((file_hash, filename))
|
| 58 |
+
keep_files2.append( filename)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
'''
|
| 62 |
+
if file_hash in hashes:
|
| 63 |
+
# Duplicate found, remove it
|
| 64 |
+
#os.remove(file_path)
|
| 65 |
+
removed_files.append(filename)
|
| 66 |
+
else:
|
| 67 |
+
hashes[file_hash] = filename
|
| 68 |
+
keep_files.append(filename)
|
| 69 |
+
'''
|
| 70 |
+
|
| 71 |
+
# Save removed files list to JSON
|
| 72 |
+
if removed_files:
|
| 73 |
+
out = {"moviename":folder,"removed_files":removed_files,"keep_files":keep_files2}
|
| 74 |
+
json_path = os.path.join(base_folder, folder+"_filter.json")
|
| 75 |
+
with open(json_path, "w") as f:
|
| 76 |
+
json.dump(out, f, indent=4)
|
| 77 |
+
print(f"Keep {len(keep_files2)} Removed {len(removed_files)} duplicate files in {folder}")
|
| 78 |
+
|
| 79 |
+
print("Duplicate removal complete.")
|
toImageHQ.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import subprocess
|
| 3 |
+
|
| 4 |
+
# Folder to save extracted frames
|
| 5 |
+
output_base = "hq_og"
|
| 6 |
+
|
| 7 |
+
# Create the base output folder if it doesn't exist
|
| 8 |
+
os.makedirs(output_base, exist_ok=True)
|
| 9 |
+
|
| 10 |
+
# List all video files in the current directory
|
| 11 |
+
video_extensions = [".mp4", ".mov", ".avi", ".mkv", ".flv"]
|
| 12 |
+
videos = [f for f in os.listdir(".") if os.path.splitext(f)[1].lower() in video_extensions]
|
| 13 |
+
|
| 14 |
+
for video in videos:
|
| 15 |
+
video_name = os.path.splitext(video)[0]
|
| 16 |
+
output_folder = os.path.join(output_base, video_name)
|
| 17 |
+
os.makedirs(output_folder, exist_ok=True)
|
| 18 |
+
|
| 19 |
+
# FFmpeg command to extract frames as high-quality WebP
|
| 20 |
+
cmd = [
|
| 21 |
+
"ffmpeg",
|
| 22 |
+
"-i", video,
|
| 23 |
+
"-vf", "fps=25", # extract 25 frames per second
|
| 24 |
+
"-vcodec", "libwebp", # use WebP encoder
|
| 25 |
+
"-lossless", "0", # 0 = lossy, 1 = lossless
|
| 26 |
+
"-q:v", "90", # quality (0 best, 100 worst)
|
| 27 |
+
"-compression_level", "2", # compression level (0–6)
|
| 28 |
+
os.path.join(output_folder, "frame_%05d.webp")
|
| 29 |
+
]
|
| 30 |
+
|
| 31 |
+
print(f"Processing {video} -> {output_folder}")
|
| 32 |
+
subprocess.run(cmd, check=True)
|
| 33 |
+
|
| 34 |
+
print("All videos processed!")
|