-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImages2Vids.py
125 lines (106 loc) · 5.08 KB
/
Images2Vids.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#IN THIS CODE WE CREATE VIDEOS STARTING FROM IMAGES
from pathlib import Path
from tqdm import tqdm
import subprocess
import os
import sys
import shutil
import numpy as np
# Define input and output paths
data_folder = Path("/home/matthias/Videos/Alice_Samara_cropped2")
output_path = Path(
"/home/matthias/Videos/Alice_Samara_Videos2/"
)
# Set frames per second for the videos (the same as the original recording)
fps = "29"
# Function to check the integrity of a video using ffprobe
def check_video_integrity(video_path):
try:
result = subprocess.run(
["ffprobe", "-v", "error", "-show_format", "-show_streams", video_path],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True,
)
return True
except subprocess.CalledProcessError as e:
print(f"Error checking video integrity: {e.stderr.decode('utf-8')}")
return False
# Function to create a video from images in a given folder
def create_video_from_images(images_folder, output_folder, video_name, fps):
video_path = output_folder / f"{video_name}.mp4"
# Only create the video if it doesn't already exist
if not video_path.exists():
# Construct the ffmpeg command
terminal_call = f"/usr/bin/ffmpeg -hwaccel cuda -r {fps} -i {images_folder.as_posix()}/image%d.jpg -pix_fmt yuv420p -c:v libx265 -crf 15 {video_path.as_posix()}"
# Run the ffmpeg command
subprocess.run(terminal_call, shell=True)
return True
else:
return False
# -loglevel panic -nostats > This is to remove the output of the ffmpeg command from the terminal, to add right after the ffmpeg command
# \"scale=-2:-2\"
# Function to search a folder for images and create videos from them
def search_folder_for_images(folder_path, output_folder, fps):
subdirs = []
# Recursively find all subdirectories containing jpg images
for subdir in folder_path.glob("**/*"):
if subdir.is_dir() and any(
file.name.endswith(".jpg") for file in subdir.glob("*")
):
subdirs.append(subdir)
# Progress bar for processing videos
with tqdm(total=len(subdirs), desc="Processing videos") as pbar:
for subdir in subdirs:
relative_subdir = subdir.relative_to(folder_path)
video_output_folder = output_folder / relative_subdir
video_output_folder.mkdir(parents=True, exist_ok=True)
video_name = relative_subdir.name
video_path = video_output_folder / f"{video_name}.mp4"
if video_path.exists():
if not check_video_integrity(video_path.as_posix()) or os.path.getsize(video_path) < 100 * 1024:
print(f"Video {video_name} is corrupted or too small.")
print(f"Removing corrupted or small video: {video_path.as_posix()}")
video_path.unlink()
# Create the video if it doesn't exist
if not video_path.exists():
create_video_from_images(subdir, video_output_folder, video_name, fps)
pbar.update(1)
# Iterate over all folders in the data_folder
for folder in data_folder.iterdir():
if folder.is_dir():
print(f"Processing folder: {folder.name}")
output_folder_name = folder.name
output_folder = output_path / f"{output_folder_name}"
output_folder.mkdir(parents=True, exist_ok=True)
processing_output_folder = output_path / f"{output_folder_name}_Processing"
# Rename output folder to indicate processing
if not processing_output_folder.exists():
output_folder.rename(processing_output_folder)
# Load the fps value from the fps.npy file in the experiment directory
# fps_file = processing_output_folder / "fps.npy"
# if fps_file.exists():
# fps = np.load(fps_file)
# fps = str(fps)
# else:
# print(f"Error: fps.npy file not found in {folder}")
# continue
fps = "29"
# Search for images and create videos
search_folder_for_images(folder, processing_output_folder, fps)
# Rename the output folder after all videos have been created
print(f"Processing of {folder.name} complete.")
new_output_folder_name = f"{output_folder_name}_Videos"
new_output_folder = output_path / new_output_folder_name
processing_output_folder.rename(new_output_folder)
# print(f"Processing of {folder.name} complete.")
# new_output_folder_name = f"{output_folder_name}_Videos"
# new_output_folder = output_path / new_output_folder_name
# shutil.move(processing_output_folder.as_posix(), new_output_folder.as_posix())
# subprocess.run(
# [
# "/home/matthias/Tracking_Analysis/Ball_Pushing/MazeRecorder/Processing/CheckVideos.sh"
# ]
# )
# TODO: Add a way to resume an aborted processing in a given folder, by checking already existing videos integrity, skipping them and processing folder not yet done.
# TODO : make the script run as a background process, always checking for non processed videos