Error with cv2 when crop, resize frames and writing a new video

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 :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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 ?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật