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
| import cv2
import numpy as np
import os
def linear_range(val1,val2, n):
pas = (val2-val1)/(n-1)
L=[]
for i in range(n):
L.append(int(val1+i*pas))
return L
def rect_morphing(rect_src:tuple, rect_dest:tuple,nb_steps:int):
L=[]
a=linear_range(rect_src[0],rect_dest[0],nb_steps)
b=linear_range(rect_src[1],rect_dest[1],nb_steps)
c=linear_range(rect_src[2],rect_dest[2],nb_steps)
d=linear_range(rect_src[3],rect_dest[3],nb_steps)
for i in range(nb_steps):
tple=(a[i],b[i],c[i],d[i])
L.append(tple)
return L
def make_movie(img: np.ndarray, rect_src: tuple, rect_dest: tuple, output_filename: str, w: int, h: int, fps: int, duration: int):
nb_image = fps * duration
nb_pas = nb_image - 1
rect_intermediaire = rect_morphing(rect_src, rect_dest, nb_pas)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video_writer = cv2.VideoWriter(output_filename, fourcc, fps, (w, h))
for rect in rect_intermediaire:
cropped_img = img[rect[1]:rect[3], rect[0]:rect[2]]
resized_img = cv2.resize(cropped_img, (w, h))
video_writer.write(resized_img)
video_writer.release()
def zoom_x2_mp4(input_filename: str, output_filename: str, duration: int):
# Chargement de l'image
im_src = cv2.imread(input_filename)
# Vérification si l'image a été chargée avec succès
if im_src is None:
print(f"Erreur: Impossible de charger l'image à partir de '{input_filename}'")
return
# Obtention des dimensions de l'image
(h, w) = im_src.shape[:2]
# Calcul des dimensions de la zone centrale zoomée
w_half, h_half = w // 2, h // 2
# Nombre de frames
fps = 30
nb_img = duration * fps
nb_pas = nb_img - 1
# Calcul des rectangles intermédiaires
rect_intermediaire = rect_morphing((0, 0, w, h),
(w_half // 2, h_half // 2, w - w_half // 2, h - h_half // 2),
nb_pas)
# Initialisation de l'objet VideoWriter pour écrire la vidéo
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_filename, fourcc, fps, (w, h))
# Boucle sur les rectangles intermédiaires pour créer la vidéo zoomée
for rect in rect_intermediaire:
cropped_img = im_src[rect[1]:rect[3], rect[0]:rect[2]]
resized_img = cv2.resize(cropped_img, (w, h))
out.write(resized_img)
# Libération de la ressource VideoWriter
out.release()
def recursive_zoom_mp4(input_prefix: str, i_max: int, output_filename: str, duration: int):
mp4_files = []
for i in range(1, i_max + 1):
input_filename = f"{input_prefix}_{i}.jpg"
output_filename_i = f"{input_prefix}_{i}.mp4"
zoom_x2_mp4(input_filename, output_filename_i, duration)
mp4_files.append(output_filename_i)
# Création du fichier de liste pour ffmpeg
list_filename = "mp4_list.txt"
with open(list_filename, 'w') as f:
for mp4_file in mp4_files:
f.write(f"file '{mp4_file}'\n")
# Concaténation des fichiers mp4 en utilisant la concaténation de FFmpeg
output_path = os.path.join(output_filename)
os.system(f'ffmpeg -f concat -safe 0 -i {list_filename} -c copy "{output_path}"')
# Suppression du fichier de liste et des fichiers vidéo temporaires
os.remove(list_filename)
for file in mp4_files:
os.remove(file) |
Partager