|
|
@ -1,6 +1,26 @@ |
|
|
|
import os |
|
|
|
import subprocess |
|
|
|
import re |
|
|
|
import shutil |
|
|
|
|
|
|
|
def convert_video(video_path, output_path): |
|
|
|
print(f"Converting video, {video_path}") |
|
|
|
|
|
|
|
# Codec that works with openframeworks! |
|
|
|
|
|
|
|
command = [ |
|
|
|
"ffmpeg", |
|
|
|
"-i", video_path, # Input file |
|
|
|
"-c:v", "libx264", # Video codec |
|
|
|
"-profile:v", "high", # Video profile |
|
|
|
"-c:a", "copy", # Copy audio without re-encoding |
|
|
|
output_path # Output file |
|
|
|
] |
|
|
|
|
|
|
|
results = subprocess.run(command, stderr=subprocess.PIPE, text=True) |
|
|
|
output = results.stderr |
|
|
|
|
|
|
|
print(f"Video converted {output}") |
|
|
|
|
|
|
|
def get_frame_rate(video_path): |
|
|
|
""" |
|
|
@ -34,7 +54,7 @@ def extract_frames_from_video(video_path, output_folder): |
|
|
|
'-i', video_path, # Input file |
|
|
|
'-vf', 'fps=1', # Extract 1 frame per second |
|
|
|
'-vsync', 'vfr', # Variable frame rate |
|
|
|
os.path.join(output_folder, 'frame_%04d.png') # Output pattern |
|
|
|
os.path.join(output_folder, 'frame_%09d.png') # Output pattern |
|
|
|
] |
|
|
|
|
|
|
|
subprocess.run(command, check=True) |
|
|
@ -49,7 +69,7 @@ def rename_frames(output_folder, frame_rate): |
|
|
|
original_frame_number = int(i * frame_rate) |
|
|
|
|
|
|
|
# Create the new filename with the original frame number |
|
|
|
new_filename = f"frame_{original_frame_number:05d}.png" |
|
|
|
new_filename = f"{original_frame_number:09d}.png" |
|
|
|
os.rename( |
|
|
|
os.path.join(output_folder, filename), |
|
|
|
os.path.join(output_folder, new_filename) |
|
|
@ -61,17 +81,32 @@ def process_videos_in_folder(folder_path): |
|
|
|
""" |
|
|
|
for filename in os.listdir(folder_path): |
|
|
|
if filename.lower().endswith(('.mp4', '.avi', '.mov', '.mkv', '.flv')): |
|
|
|
|
|
|
|
video_path = os.path.join(folder_path, filename) |
|
|
|
|
|
|
|
output_folder = os.path.join(folder_path, os.path.splitext(filename)[0]) # Create a folder for each video |
|
|
|
|
|
|
|
# New location for .mp4 |
|
|
|
video_copy_location = os.path.join(output_folder, filename) |
|
|
|
|
|
|
|
# Create frames folder |
|
|
|
frames_folder = os.path.join(output_folder, "frames") |
|
|
|
|
|
|
|
if not os.path.exists(output_folder): |
|
|
|
os.mkdir(output_folder) |
|
|
|
|
|
|
|
if not os.path.exists(frames_folder): |
|
|
|
os.mkdir(frames_folder) |
|
|
|
|
|
|
|
print(f'Processing video: {video_path}') |
|
|
|
|
|
|
|
try: |
|
|
|
frame_rate = get_frame_rate(video_path) |
|
|
|
convert_video(video_path, video_copy_location) |
|
|
|
frame_rate = get_frame_rate(video_copy_location) |
|
|
|
print(f'Original frame rate: {frame_rate} fps') |
|
|
|
extract_frames_from_video(video_path, output_folder) |
|
|
|
rename_frames(output_folder, frame_rate) |
|
|
|
print(f'Frames saved and renamed to: {output_folder}') |
|
|
|
extract_frames_from_video(video_copy_location, frames_folder) |
|
|
|
rename_frames(frames_folder, frame_rate) |
|
|
|
print(f'Frames saved and renamed to: {frames_folder}') |
|
|
|
except ValueError as e: |
|
|
|
print(f'Error processing video {video_path}: {e}') |
|
|
|
|
|
|
|