Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in-browser AVI viewing broken #1908

Open
bendichter opened this issue Mar 26, 2024 · 5 comments
Open

in-browser AVI viewing broken #1908

bendichter opened this issue Mar 26, 2024 · 5 comments

Comments

@waxlamp
Copy link
Member

waxlamp commented Apr 16, 2024

Hey Ben, thanks for this issue report.

I'm not sure exactly why the video player is broken, though I suspect it's that the video is not encoded in a way that the browser can decode with its built-in codecs.

But I did download the whole video file, only to discover that it's 30 seconds of blank frames with no audio track. Furthermore, it seems to be supplying every frame explicitly (the whole video consists of the same (blank) video frame, repeated), causing a big file size to encode (literally) nothing.

Did something maybe go wrong during acquisition of the data or preparation of this video?

@bendichter
Copy link
Contributor Author

@pauladkisson do you know what might be going on here?

@pauladkisson
Copy link

I'm not sure exactly why the video player is broken, though I suspect it's that the video is not encoded in a way that the browser can decode with its built-in codecs.

This doesn't surprise me. I was unable to play the videos appropriately with video player applications, but it read fine using cv2.

But I did download the whole video file, only to discover that it's 30 seconds of blank frames with no audio track. Furthermore, it seems to be supplying every frame explicitly (the whole video consists of the same (blank) video frame, repeated), causing a big file size to encode (literally) nothing.

This is strange though...I'll download it myself and take a look.

@pauladkisson
Copy link

Ok, I double checked and this file is fine -- looks like a normal depth video from this dataset (use code below to read it yourself).

import cv2

raw_video = cv2.VideoCapture('path/to/0abe7609-af66-4d81-865e-a20a4b414908_external_file_0.avi')

while True:
    # Read a frame from the video
    ret, frame = raw_video.read()

    # If the frame is not read successfully, the video has ended
    if not ret:
        break

    frame = frame / frame.max() # Normalize the frame

    # Display the frame in a window
    cv2.imshow('Frame', frame)

    # Wait for a key press to exit
    if cv2.waitKey(25) & 0xFF == ord('q'):
        break

# Release the video capture and close the window
raw_video.release()
cv2.destroyAllWindows()

@bendichter
Copy link
Contributor Author

ah, I see. It's an RGB video where they all have the same value and the max of that value is ~17 (/255) so all the frames look black. Also, this would be 1/3 the size if stored properly as a grayscale. Here's code that does it, and rescales the values so you can actually see something, but it takes like 45 minutes for a single 30-minute video:

import cv2
from tqdm import tqdm

def convert_video_to_grayscale(input_path: str, output_path: str):
    """
    Convert a video to grayscale and save it.

    Parameters
    ----------
    input_path : str
        The path to the input video file.
    output_path : str
        The path where the grayscale video will be saved.

    Returns
    -------
    None
    """
    # Open the input video
    raw_video = cv2.VideoCapture(input_path)
    if not raw_video.isOpened():
        raise IOError(f"Cannot open video {input_path}")

    # Get the video frame width and height
    frame_width = int(raw_video.get(cv2.CAP_PROP_FRAME_WIDTH))
    frame_height = int(raw_video.get(cv2.CAP_PROP_FRAME_HEIGHT))
    fps = raw_video.get(cv2.CAP_PROP_FPS)
    total_frames = int(raw_video.get(cv2.CAP_PROP_FRAME_COUNT))

    # Define the codec and create VideoWriter object
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height), isColor=False)

    # Initialize tqdm progress bar
    pbar = tqdm(total=total_frames, desc="Converting video")

    while True:
        # Read a frame from the video
        ret, frame = raw_video.read()

        # If the frame is not read successfully, the video has ended
        if not ret:
            pbar.close()
            break

        # Normalize the frame's brightness if needed
        frame = frame * 11

        # Convert the frame to grayscale
        gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        # Write the grayscale frame to the output video
        out.write(gray_frame)

        # Update the progress bar
        pbar.update(1)

        # Display the frame in a window (optional)
        cv2.imshow('Frame', gray_frame)

        # Wait for a key press to exit
        if cv2.waitKey(25) & 0xFF == ord('q'):
            pbar.close()
            break

    # Release the video capture and close the window
    raw_video.release()
    out.release()
    cv2.destroyAllWindows()

# Example usage:
input_video_path = 'path/to/4b984a03-a045-47ab-9669-b8fa6a22cfd7_external_file_0.avi'
output_video_path = 'path/to/4b984a03-a045-47ab-9669-b8fa6a22cfd7_external_file_0_bw.avi'
convert_video_to_grayscale(input_video_path, output_video_path)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants