0% found this document useful (0 votes)
13 views7 pages

Tutorial7 3D Video Coding

This tutorial teaches how to construct 3D videos by extracting left-eye and right-eye views from a 3D video and encoding them independently using H.264. It provides step-by-step instructions for downloading a video, extracting frames, separating channels, and compressing the views into videos. The final output includes downloadable zip files of the frames and compressed videos for viewing with 3D glasses.

Uploaded by

Minh Đức
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views7 pages

Tutorial7 3D Video Coding

This tutorial teaches how to construct 3D videos by extracting left-eye and right-eye views from a 3D video and encoding them independently using H.264. It provides step-by-step instructions for downloading a video, extracting frames, separating channels, and compressing the views into videos. The final output includes downloadable zip files of the frames and compressed videos for viewing with 3D glasses.

Uploaded by

Minh Đức
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

TUTORIAL 7 – 3D VIDEO CONTRUCTION AND CODING

Objectives:

• To learn how a 3D video is constructed from left- and right-view video components
• To encode the components independently using H26

I. Frame extraction
To extract the left-eye and right-eye views from 3D video, we need to process the video
and separate the red channel (left-eye view) and the cyan channel (right-eye view).

Here is a step-by-step approach for the task:

1. Download the video (from YouTube or any URL).


2. Extract the frames from the video.
3. Separate the channels:
a. Left-eye view: This is typically the red channel.
b. Right-eye view: This is typically the cyan channel (combined green and
blue).
4. Save the left-eye and right-eye images from each frame.

🎬 Full Colab Program to Extract Left-Eye and Right-Eye Views from a 3D


Video

🔧 STEP 1: Install Necessary Dependencies

!pip install yt-dlp opencv-python matplotlib


!pip install imageio

📥 STEP 2: Download the YouTube Video

We'll use yt-dlp to download the YouTube video directly.


import yt_dlp

# Replace the URL with the correct one for the "Roller Coaster Dinosaur T-Rex 3D" video
url = "https://www.youtube.com/watch?v=hdUCJNJgBD8" # Link to the 3D video
output_file = "roller_coaster_3d.mp4"

ydl_opts = {
'format': 'best[ext=mp4]',
'outtmpl': output_file,
}

with yt_dlp.YoutubeDL(ydl_opts) as ydl:


ydl.download([url])

print(f"Downloaded video as {output_file}")

🎥 STEP 3: Extract Frames from the Video

import cv2
import os

# Create output directories for the left-eye and right-eye views


os.makedirs("left_eye", exist_ok=True)
os.makedirs("right_eye", exist_ok=True)

# Open the video file


cap = cv2.VideoCapture(output_file)
fps = cap.get(cv2.CAP_PROP_FPS)
frame_count = 0

while True:
ret, frame = cap.read()
if not ret:
break

# Split the frame into channels


left_eye_frame = frame.copy() # Left-eye view (red channel)
right_eye_frame = frame.copy() # Right-eye view (cyan)

# Left-eye: Keep only the red channel (typically for the left-eye view)
left_eye_frame[:, :, 1] = 0 # Remove green channel
left_eye_frame[:, :, 2] = 0 # Remove blue channel

# Right-eye: Keep only the green and blue channels (typically for the right-eye view)
right_eye_frame[:, :, 0] = 0 # Remove red channel

# Save the left-eye and right-eye frames as images


cv2.imwrite(f"left_eye/frame_{frame_count:04d}.png", left_eye_frame)
cv2.imwrite(f"right_eye/frame_{frame_count:04d}.png", right_eye_frame)

frame_count += 1

cap.release()
print(f"Extracted {frame_count} frames.")

💾 STEP 4: Download the Left-Eye and Right-Eye Frames

You can download the left-eye and right-eye frames manually or create a zip file to
download the entire set of frames.

import shutil

# Create a zip file for the extracted frames


shutil.make_archive("left_eye_frames", 'zip', "left_eye")
shutil.make_archive("right_eye_frames", 'zip', "right_eye")

# Provide download links


from google.colab import files
files.download("left_eye_frames.zip")
files.download("right_eye_frames.zip")
✅ Summary:

• This program downloads the 3D anaglyph video.


• It extracts frames, then separates the frames into left-eye (red channel) and right-
eye (cyan channel).
• Saves the left-eye and right-eye frames as separate images.
• It generates zip files of the extracted frames, which you can download from Colab.

II. Component compression


To compress the left-eye and right-eye videos into H.264 format using FFmpeg in Google
Colab, we can follow these steps:

Steps:

1. Create the video from extracted frames (for both left-eye and right-eye).
2. Compress both videos using the H.264 codec.

🎬 Full Colab Program to Compress Left-Eye and Right-Eye Views using


H.264

🔧 STEP 1: Install Necessary Dependencies

Make sure you have all dependencies for FFmpeg and video processing installed:

!pip install yt-dlp opencv-python matplotlib


!pip install imageio

📥 STEP 2: Download the YouTube Video (if you haven't already)

import yt_dlp

# Replace with the URL for the "Roller Coaster Dinosaur T-Rex 3D" video
url = "https://www.youtube.com/watch?v=hdUCJNJgBD8" # Link to the 3D video
output_file = "roller_coaster_3d.mp4"
ydl_opts = {
'format': 'best[ext=mp4]',
'outtmpl': output_file,
}

with yt_dlp.YoutubeDL(ydl_opts) as ydl:


ydl.download([url])

print(f"Downloaded video as {output_file}")

🎥 STEP 3: Extract Frames and Create Left-Eye and Right-Eye Videos

If you've already extracted frames in the previous steps, we can move directly to this
compression part.

Here’s how to create the left-eye and right-eye videos:

import cv2
import os

# Directories for the frames


left_eye_dir = "left_eye"
right_eye_dir = "right_eye"

# Ensure directories exist for video generation


os.makedirs(left_eye_dir, exist_ok=True)
os.makedirs(right_eye_dir, exist_ok=True)

# Open the original 3D video for frame extraction


cap = cv2.VideoCapture(output_file)
fps = cap.get(cv2.CAP_PROP_FPS)
frame_count = 0

# Create left-eye and right-eye view frames


while True:
ret, frame = cap.read()
if not ret:
break

left_eye_frame = frame.copy()
right_eye_frame = frame.copy()

# Left-eye: Keep only the red channel


left_eye_frame[:, :, 1] = 0
left_eye_frame[:, :, 2] = 0

# Right-eye: Keep only the green and blue channels


right_eye_frame[:, :, 0] = 0

# Save the left-eye and right-eye frames


cv2.imwrite(f"{left_eye_dir}/frame_{frame_count:04d}.png", left_eye_frame)
cv2.imwrite(f"{right_eye_dir}/frame_{frame_count:04d}.png", right_eye_frame)

frame_count += 1

cap.release()
print(f"Extracted {frame_count} frames.")

🖥️ STEP 4: Create Left-Eye and Right-Eye Videos

Now, we use FFmpeg to compress the frames and generate H.264 encoded videos.

# Create left-eye and right-eye videos using H.264 compression

!ffmpeg -framerate {fps} -i {left_eye_dir}/frame_%04d.png -c:v libx264 -pix_fmt yuv420p


left_eye_video.mp4 -y
!ffmpeg -framerate {fps} -i {right_eye_dir}/frame_%04d.png -c:v libx264 -pix_fmt yuv420p
right_eye_video.mp4 -y
📤 STEP 5: Download the Compressed Videos

Finally, download the compressed left-eye and right-eye videos:

from google.colab import files

files.download("left_eye_video.mp4")
files.download("right_eye_video.mp4")

✅ Summary

• This script downloads the YouTube video, extracts the frames, separates them into
left-eye and right-eye views, and then compresses them into H.264 video format.
• You can download both the left-eye and right-eye videos for viewing with red/cyan
3D glasses.
• The compression process uses FFmpeg with the libx264 codec.

You might also like