Created
February 7, 2024 03:45
-
-
Save JupyterJones/b82abe53df249ebb73bf79357b49d238 to your computer and use it in GitHub Desktop.
Create Zoom Videos from a Directory of Images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import numpy as np | |
import logging | |
import glob | |
import random | |
import uuid | |
# Set up logging | |
logging.basicConfig(level=logging.INFO, format='%(levelname)s - %(message)s') | |
def zoom_pan_nonlinear1(input_path, output_path): | |
try: | |
# Define a non-linear path (in this case, a bezier curve) | |
path = lambda t: (100 * np.sin(2 * np.pi * t), 50 * np.cos(2 * np.pi * t)) | |
# Build the updated ffmpeg command with zoom pan effect | |
ffmpeg_cmd = [ | |
'ffmpeg', | |
'-i', input_path, | |
'-vf', "scale=24000:-1,zoompan=z='if(lte(zoom,1.0),1.5,max(1.001,zoom-0.0003))':x=iw/2-(iw/zoom/2):y=ih/2-(ih/zoom/2):d=25*60:s=512x768:fps=60", | |
'-c:a', 'aac', | |
'-c:v', 'libx264', | |
'-y', # Overwrite output file if it already exists | |
output_path | |
] | |
# Run the ffmpeg command using subprocess | |
subprocess.run(ffmpeg_cmd, check=True) | |
logging.info(f"Zoom pan effect with a non-linear path applied. Output saved to {output_path}") | |
except subprocess.CalledProcessError as e: | |
logging.error(f"An error occurred while running ffmpeg: {e}") | |
except Exception as e: | |
logging.error(f"An unexpected error occurred: {str(e)}") | |
def zoom_pan_nonlinear2(input_path, output_path): | |
try: | |
# Define a non-linear path (in this case, a bezier curve) | |
path = lambda t: (100 * np.sin(2 * np.pi * t), 50 * np.cos(2 * np.pi * t)) | |
# Build the updated ffmpeg command with zoom pan effect | |
#ffmpeg -loop 1 -framerate 60 -i yodayofeb6/00047.jpg -vf "scale=8000:-1,zoompan=z='zoom+0.001':x=iw/2-(iw/zoom/2):y=ih/2-(ih/zoom/2):d=35*60:s=512x768:fps=60" -t 35 -c:v libx264 -pix_fmt yuv420p -y output3. | |
ffmpeg_cmd = [ | |
'ffmpeg','-loop','1', | |
'-i', input_path, | |
'-vf', "scale=8000:-1,zoompan=z='zoom+0.001':x=iw/2-(iw/zoom/2):y=ih/2-(ih/zoom/2):d=30*60:s=512x768:fps=60", | |
'-t','30', | |
'-c:a', 'aac', | |
'-c:v', 'libx264', | |
'-y', # Overwrite output file if it already exists | |
output_path | |
] | |
# Run the ffmpeg command using subprocess | |
subprocess.run(ffmpeg_cmd, check=True) | |
logging.info(f"Zoom pan effect with a non-linear path applied. Output saved to {output_path}") | |
except subprocess.CalledProcessError as e: | |
logging.error(f"An error occurred while running ffmpeg: {e}") | |
except Exception as e: | |
logging.error(f"An unexpected error occurred: {str(e)}") | |
# Example usage | |
if __name__ == "__main__": | |
for i in range(0,20): | |
input_image_path = random.choice(glob.glob("yodayofeb6/*.jpg")) | |
output_video_path = "Videos/"+str(uuid.uuid4()) + "+.mp4" | |
zoom_pan_nonlinear1(input_image_path, output_video_path) | |
input_image_path = random.choice(glob.glob("yodayofeb6/*.jpg")) | |
output_video_path = "Videos/"+str(uuid.uuid4()) + "++.mp4" | |
zoom_pan_nonlinear2(input_image_path, output_video_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment