Skip to content

Instantly share code, notes, and snippets.

@ximeg
Created January 31, 2024 23:35
Show Gist options
  • Save ximeg/394687265e4c3862ae67dcf3d003161c to your computer and use it in GitHub Desktop.
Save ximeg/394687265e4c3862ae67dcf3d003161c to your computer and use it in GitHub Desktop.
Convert a number of individual frames to a compressed MP4 movie with ffmpeg on Linux from Python
# https://chat.openai.com/c/cff8e0fc-20df-4425-9e2b-7aa8228ab7a1
## Install ffmpeg on RHEL8: https://www.benholcomb.com/ffmpeg-on-rhel8/
## After that
# sudo yum install python3-devel
## Create virtual environment
# python3 -m venv path/to/venv
# cd path/to/venv
# bin/activate
## install imageio and ffmpeg wrapper
# pip install imageio[ffmpeg]
import os
import imageio
input_folder = '/research_jude/common/Roman/frames/'
output_file = '/research_jude/common/Roman/ffmpeg_output.mp4'
# List all image files in the input folder
image_files = [f for f in os.listdir(input_folder) if f.endswith('.tif')]
image_files.sort() # Ensure the frames are in the correct order
# Create a writer for the output video file with custom FFmpeg options
writer = imageio.get_writer(output_file, codec='libx264', ffmpeg_params=['-crf', '30'])
# Iterate through image files and write to the output file
for image_file in image_files:
image_path = os.path.join(input_folder, image_file)
frame = imageio.imread(image_path)
writer.append_data(frame)
# Close the writer to finalize the output file
writer.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment