Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import sys
- import subprocess
- import glob
- import time
- def get_average_fps(video_path):
- """Gets the average FPS of a video using ffprobe."""
- cmd = ['ffprobe', '-v', 'error', '-select_streams', 'v:0', '-show_entries', 'stream=avg_frame_rate',
- '-of', 'default=noprint_wrappers=1:nokey=1', video_path]
- process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
- output, error = process.communicate()
- if process.returncode != 0:
- raise RuntimeError(f'ffprobe error: {error}')
- num, den = map(int, output.strip().split('/'))
- return num / den
- def process_video(input_video_path, output_dir):
- """Processes a video using Topaz Photo AI and ffmpeg."""
- video_name = os.path.splitext(os.path.basename(input_video_path))[0]
- temp_dir = os.path.join(output_dir, "temp_frames", video_name)
- upscaled_dir = os.path.join(output_dir, "temp_frames", video_name + "_upscaled")
- os.makedirs(temp_dir, exist_ok=True)
- os.makedirs(upscaled_dir, exist_ok=True)
- avg_framerate = get_average_fps(input_video_path)
- # Extract frames
- subprocess.run([
- 'ffmpeg', '-i', input_video_path, '-vf', f'fps={avg_framerate}', f'{temp_dir}/frame%04d.bmp'
- ])
- # Upscale frames using Topaz Photo AI
- subprocess.run([
- 'tpai', '--format', 'tiff', '--bit-depth', '16', '-o', upscaled_dir, temp_dir
- ], check=True) # check=True raises an exception if the command fails
- # Combine upscaled frames back into video
- output_video_path = os.path.join(output_dir, video_name + "_upscaled.mov")
- subprocess.run([
- 'ffmpeg', '-y', '-framerate', str(avg_framerate),
- '-i', f'{upscaled_dir}/frame%04d.tiff', # Input TIFF files
- '-i', input_video_path, '-map', '0:v', '-map', '1:a',
- '-c:v', 'ffv1', '-pix_fmt', 'rgb48',
- '-metadata:s:v:0', 'encoder=FFV1', '-level', '3', '-g', '1',
- '-slices', '24', '-slicecrc', '1', output_video_path
- ])
- # Clean up temporary frames
- subprocess.run(['rm', '-r', temp_dir])
- subprocess.run(['rm', '-r', upscaled_dir])
- if __name__ == "__main__":
- input_dir = "." # Current directory
- output_dir = "output"
- os.makedirs(output_dir, exist_ok=True)
- # Find all video files in the input directory using ffmpeg
- input_video_paths = []
- for file in os.listdir(input_dir):
- file_path = os.path.join(input_dir, file)
- try:
- probe_cmd = ['ffprobe', '-v', 'error', '-select_streams', 'v:0', '-show_entries', 'stream=codec_type',
- '-of', 'default=noprint_wrappers=1:nokey=1', file_path]
- process = subprocess.Popen(probe_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
- output, error = process.communicate()
- if process.returncode == 0 and output.strip() == 'video':
- input_video_paths.append(file_path)
- except Exception as e:
- print(f"Error checking file {file_path}: {e}")
- # Process each video file and store processing times
- processing_times = []
- for input_video_path in input_video_paths:
- start_time = time.time()
- print(f"Processing video: {input_video_path}")
- process_video(input_video_path, output_dir)
- end_time = time.time()
- processing_time = end_time - start_time
- processing_times.append((input_video_path, processing_time))
- # Print processing times after all videos are processed
- print("\nProcessing times:")
- for input_video_path, processing_time in processing_times:
- print(f"{input_video_path}: {processing_time:.2f} seconds")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement