-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_to_video.py
More file actions
90 lines (74 loc) · 2.66 KB
/
image_to_video.py
File metadata and controls
90 lines (74 loc) · 2.66 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
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
import subprocess
import os
import sys
from PIL import Image
class ImageToVideoConverter:
@staticmethod
def png_to_mp4(
fold,
title="video",
fps=36,
digit_format="04d",
res=None,
resize_factor=1,
custom_bitrate=None,
extension=".jpg",
dic_guardado='./',
):
files = [f for f in os.listdir(fold) if f.endswith(extension)]
files.sort(key=lambda x: int(x.split("_")[-1].split(".")[0]))
print(files)
if not files:
raise ValueError("No PNG files found in the specified folder.")
im = Image.open(os.path.join(fold, files[0]))
resx, resy = im.size
if res is not None:
resx, resy = res
else:
resx = int(resize_factor * resx)
resy = int(resize_factor * resy)
resx += resx % 2 # Ensuring even dimensions
resy += resy % 2
basename = os.path.splitext(files[0])[0].split("_")[0]
ffmpeg_path = "ffmpeg"
abs_path = os.path.abspath(fold)
parent_folder = os.path.dirname(abs_path) + os.sep
output_file = os.path.join(parent_folder, f"{title}.mp4")
output_file = dic_guardado + title + ".mp4"
crf = 1 # Lower for higher quality, higher for lower quality
bitrate = custom_bitrate if custom_bitrate else "5000k"
preset = "slow"
tune = "film"
command = f'{ffmpeg_path} -y -r {fps} -i {os.path.join(fold, f"{basename}_%{digit_format}{extension}")} -c:v libx264 -profile:v high -crf {crf} -preset {preset} -tune {tune} -b:v {bitrate} -pix_fmt yuv420p -vf scale={resx}:{resy} {output_file}'
try:
result = subprocess.run(
command,
shell=True,
stdout=subprocess.PIPE,
check=True,
)
print('Done!')
except subprocess.CalledProcessError as e:
print("Error during video conversion:", e)
if __name__ == "__main__":
if len(sys.argv) != 6:
print(
"Uso: python image_to_video.py directorio_images/ fps directorio_video titulo resize"
)
sys.exit(1)
dic = str(sys.argv[1])
fps = sys.argv[2]
dic_guardado = str(sys.argv[3])
titulo = sys.argv[4]
resize = float(sys.argv[5])
if not (os.path.exists(dic_guardado)):
os.mkdir(dic_guardado)
ImageToVideoConverter.png_to_mp4(
dic,
extension=".jpg",
digit_format="01d",
fps=fps,
title=titulo,
resize_factor=resize,
dic_guardado='./',
)