Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23247

OpenCV: Why is the Entire Video Displaying the Same Frame?

$
0
0

Code:-

import cv2import time# Load the videovideo_path = "g.mp4"  # Replace "g.mp4" with the path to your video filecap = cv2.VideoCapture(video_path)# Get video propertiesfps = cap.get(cv2.CAP_PROP_FPS)total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))duration = total_frames / fps# Define the duration for decreasing opacity (e.g., last 5 seconds)opacity_duration = 5  # secondsopacity_start_time = duration - opacity_duration# Output videooutput_path = "output_video.mp4"fourcc = cv2.VideoWriter_fourcc(*'mp4v')out = cv2.VideoWriter(output_path, fourcc, fps, (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))))# Variables for time estimationstart_time = time.time()frame_count = 0# Process each framewhile cap.isOpened():    ret, frame = cap.read()    if not ret:        break    # Calculate current time    current_time = frame_count / fps    # Decrease opacity gradually for the last 5 seconds    if current_time >= opacity_start_time:        alpha = 1 - (current_time - opacity_start_time) / opacity_duration        frame = cv2.addWeighted(frame, alpha, frame, 0, 0)    # Write the frame to the output video    out.write(frame)    # Update frame count    frame_count += 1    # Calculate elapsed time and estimate remaining time    elapsed_time = time.time() - start_time    progress = frame_count / total_frames    remaining_time = (elapsed_time / progress) - elapsed_time    # Print time estimation    print(f"Progress: {progress * 100:.2f}%, Elapsed Time: {elapsed_time:.2f}s, Remaining Time: {remaining_time:.2f}s", end='\r')    # Display the frame    cv2.imshow('Frame', frame)    if cv2.waitKey(1) & 0xFF == ord('q'):        break# Release video capture and writercap.release()out.release()# Close all OpenCV windowscv2.destroyAllWindows()

I'm working on a video processing script using OpenCV in Python. The script is designed to process each frame of a video, gradually decreasing the opacity of the frames during the last 5 seconds of the video. However, when I run the script with a sample video (g.mp4), the output video (output_video.mp4) ends up showing the same frame repeatedly throughout the entire duration instead of playing through the video content as expected.

I've attempted to process the video frames using OpenCV in Python. I've carefully implemented the logic to gradually decrease the opacity of frames during the last 5 seconds of the video. I've ensured that the input video is correctly loaded and its properties are retrieved accurately. Additionally, I've monitored the frame processing progress and estimated the time remaining during execution.I expect the output video to play through the entire content of the input video (g.mp4) while gradually decreasing the opacity of frames during the last 5 seconds. Each frame should be processed sequentially, resulting in a smooth transition of opacity towards the end of the video. However, the current issue is that the output video ends up showing the same frame repeatedly instead of playing through the video content as intended.

Thank you in advance for any assistance or insights provided!


Viewing all articles
Browse latest Browse all 23247

Trending Articles