Skip to content

Instantly share code, notes, and snippets.

@daitomanabe
Created June 1, 2023 12:17
Show Gist options
  • Save daitomanabe/36242d846d59b27a46af3a5f1c6fe376 to your computer and use it in GitHub Desktop.
Save daitomanabe/36242d846d59b27a46af3a5f1c6fe376 to your computer and use it in GitHub Desktop.
mov_to_jpeg
import os
import sys
from moviepy.editor import VideoFileClip
from PIL import Image
# Directory containing the .mov files to be converted
input_dir = sys.argv[1]
# Base directory for outputting the image files
output_base_dir = sys.argv[2]
# Search for all .mov files in the input directory
for filename in os.listdir(input_dir):
if filename.endswith(('.mov', '.mp4')):
# Get the full path
movie_path = os.path.join(input_dir, filename)
# Load the movie using moviepy
clip = VideoFileClip(movie_path)
# Create an output directory for each movie file
output_dir = os.path.join(output_base_dir, os.path.splitext(filename)[0])
os.makedirs(output_dir, exist_ok=True)
# Frame number
frame_no = 0
# Write each frame of the movie to an image file
for frame in clip.iter_frames():
# Generate image file name (includes frame number)
image_filename = f'frame_{frame_no}.jpeg'
# Generate the full path of the image file
image_path = os.path.join(output_dir, image_filename)
# Write the frame to an image file
frame_image = Image.fromarray(frame)
frame_image.save(image_path, 'JPEG', quality=50)
# Update the frame number
frame_no += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment