-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_timelapse.py
More file actions
38 lines (31 loc) · 1.08 KB
/
create_timelapse.py
File metadata and controls
38 lines (31 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import os
import subprocess
import re
def natural_sort_key(s):
return [int(text) if text.isdigit() else text.lower()
for text in re.split('([0-9]+)', s)]
def create_timelapse_video(image_folder, output_video):
# Get all jpg files and sort them naturally
images = [img for img in os.listdir(image_folder) if img.endswith(".jpg")]
images.sort(key=natural_sort_key)
# Create a text file with the correct order of frames
with open('frames.txt', 'w') as f:
for img in images:
f.write(f"file '{os.path.join(image_folder, img)}'\n")
# Use the text file as input for ffmpeg
subprocess.run([
"ffmpeg",
"-f", "concat",
"-safe", "0",
"-i", "frames.txt",
"-framerate", "30",
"-c:v", "libx264",
"-pix_fmt", "yuv420p",
"-preset", "ultrafast",
output_video
])
# Clean up the temporary file
os.remove('frames.txt')
print(f"Timelapse video saved as {output_video}")
if __name__ == "__main__":
create_timelapse_video("./timelapse_images", "timelapsed_imgs.mp4")