Created
October 9, 2023 05:54
-
-
Save raldone01/f6b472f0652ded3693fae85923a21a1b to your computer and use it in GitHub Desktop.
multivid.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import cv2 | |
import numpy as np | |
import os | |
import sys | |
import logging # Step 2: Import the logging library | |
# Initialize logging | |
logging.basicConfig(level=logging.INFO) | |
def main(): | |
parser = argparse.ArgumentParser(description='Interlace two video files') | |
parser.add_argument('--files', required=True, help='The video files to interlace') | |
parser.add_argument('--output', required=True, help='output video file') | |
parser.add_argument('--width', type=int, required=True, help='output video width') | |
parser.add_argument('--height', type=int, required=True, help='output video height') | |
args = parser.parse_args() | |
logging.info('Arguments parsed.') # Step 3: Logging | |
files = args.files.split(',') | |
for file in files: | |
if not os.path.exists(file): | |
logging.error(f'File {file} does not exist') # Step 3: Logging | |
sys.exit(1) | |
caps = [cv2.VideoCapture(file) for file in files] | |
for cap in caps: | |
if not cap.isOpened(): | |
logging.error(f'Could not open {file}') # Step 3: Logging | |
sys.exit(1) | |
logging.info('Video files opened successfully.') # Step 3: Logging | |
fourcc = cv2.VideoWriter_fourcc(*'mp4v') | |
out = cv2.VideoWriter(args.output, fourcc, 20.0, (args.width, args.height)) | |
logging.info(f'Starting to write to output file {args.output}.') # Step 3: Logging | |
frame_counter = 0 | |
while True: | |
ret, frame = caps[frame_counter % len(caps)].read() | |
if not ret: | |
break | |
frame = cv2.resize(frame, (args.width, args.height)) | |
out.write(frame) | |
frame_counter += 1 | |
logging.info(f'Frame {frame_counter} written to output file.') # Step 3: Logging | |
for cap in caps: | |
cap.release() | |
out.release() | |
logging.info('Processing completed. Output video file created.') # Step 3: Logging | |
# Entry point of the program | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment