Hello I’m using the following script where I (in the beginning) crop and resize the first 10 frames and write a new video with the result :
<code>import cv2
# Chemin vers le fichier vidéo d'entrée
input_file = "z20240510 output20 old3 01 pour cropredim.mp4"
# Ouvrir la vidéo d'entrée
cap = cv2.VideoCapture(input_file)
# Récupérer les propriétés de la vidéo d'entrée
fps = cap.get(cv2.CAP_PROP_FPS)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# length = int(cap.get(cv2.CV_CAP_PROP_FRAME_COUNT))
# Coordonnées du coin supérieur gauche du rectangle de recadrage
x, y = 0, 0
# Largeur et hauteur du rectangle de recadrage
crop_width, crop_height = 1034, 1148
crop_width_end = crop_width // 10
crop_height_end = crop_height // 10
# Créer un objet VideoWriter pour écrire la vidéo de sortie
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
output_file = "z20240510 output20 old3 01 cropredimed.mp4"
out = cv2.VideoWriter(output_file, fourcc, fps, (crop_width, crop_height))
crop_width = 1
crop_height = 1
# Lire chaque frame de la vidéo d'entrée et recadrer
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
if crop_width > 10:
break
# Recadrer la frame
cropped_frame = frame[y:y+crop_height, x:x+crop_width]
# Recadrer l'image
cropped_frame = frame[y:y+crop_height, x:x+crop_width]
# Redimensionner le cropped_frame à la taille de l'image originale
resized_frame = cv2.resize(cropped_frame, (width, height))
# Écrire la frame recadrée dans la vidéo de sortie
out.write(resized_frame)
# Définir les coordonnées du crop (exemple)
crop_width -= 1
crop_height -= 1
# Libérer les ressources
cap.release()
out.release()
print("done")
</code>
<code>import cv2
# Chemin vers le fichier vidéo d'entrée
input_file = "z20240510 output20 old3 01 pour cropredim.mp4"
# Ouvrir la vidéo d'entrée
cap = cv2.VideoCapture(input_file)
# Récupérer les propriétés de la vidéo d'entrée
fps = cap.get(cv2.CAP_PROP_FPS)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# length = int(cap.get(cv2.CV_CAP_PROP_FRAME_COUNT))
# Coordonnées du coin supérieur gauche du rectangle de recadrage
x, y = 0, 0
# Largeur et hauteur du rectangle de recadrage
crop_width, crop_height = 1034, 1148
crop_width_end = crop_width // 10
crop_height_end = crop_height // 10
# Créer un objet VideoWriter pour écrire la vidéo de sortie
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
output_file = "z20240510 output20 old3 01 cropredimed.mp4"
out = cv2.VideoWriter(output_file, fourcc, fps, (crop_width, crop_height))
crop_width = 1
crop_height = 1
# Lire chaque frame de la vidéo d'entrée et recadrer
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
if crop_width > 10:
break
# Recadrer la frame
cropped_frame = frame[y:y+crop_height, x:x+crop_width]
# Recadrer l'image
cropped_frame = frame[y:y+crop_height, x:x+crop_width]
# Redimensionner le cropped_frame à la taille de l'image originale
resized_frame = cv2.resize(cropped_frame, (width, height))
# Écrire la frame recadrée dans la vidéo de sortie
out.write(resized_frame)
# Définir les coordonnées du crop (exemple)
crop_width -= 1
crop_height -= 1
# Libérer les ressources
cap.release()
out.release()
print("done")
</code>
import cv2
# Chemin vers le fichier vidéo d'entrée
input_file = "z20240510 output20 old3 01 pour cropredim.mp4"
# Ouvrir la vidéo d'entrée
cap = cv2.VideoCapture(input_file)
# Récupérer les propriétés de la vidéo d'entrée
fps = cap.get(cv2.CAP_PROP_FPS)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# length = int(cap.get(cv2.CV_CAP_PROP_FRAME_COUNT))
# Coordonnées du coin supérieur gauche du rectangle de recadrage
x, y = 0, 0
# Largeur et hauteur du rectangle de recadrage
crop_width, crop_height = 1034, 1148
crop_width_end = crop_width // 10
crop_height_end = crop_height // 10
# Créer un objet VideoWriter pour écrire la vidéo de sortie
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
output_file = "z20240510 output20 old3 01 cropredimed.mp4"
out = cv2.VideoWriter(output_file, fourcc, fps, (crop_width, crop_height))
crop_width = 1
crop_height = 1
# Lire chaque frame de la vidéo d'entrée et recadrer
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
if crop_width > 10:
break
# Recadrer la frame
cropped_frame = frame[y:y+crop_height, x:x+crop_width]
# Recadrer l'image
cropped_frame = frame[y:y+crop_height, x:x+crop_width]
# Redimensionner le cropped_frame à la taille de l'image originale
resized_frame = cv2.resize(cropped_frame, (width, height))
# Écrire la frame recadrée dans la vidéo de sortie
out.write(resized_frame)
# Définir les coordonnées du crop (exemple)
crop_width -= 1
crop_height -= 1
# Libérer les ressources
cap.release()
out.release()
print("done")
But when reading the result with vlc I get many many errors like this :
Votre média d’entrée ne peut être ouvert:
VLC ne peut pas ouvrir « file:///X:/ffmpeg/test/z20240510_output20_old3_01_cropredimed.mp4 ». Vérifier les messages du journal pour plus de détails.
It can’t open the file. can someone help me to understand why ?