Skip to main content

cap_from_youtube

Get an OpenCV video capture from an YouTube video URL

PyPI

Why

  • pafy is widely used to get the video URL from a YouTube video URL, but since it uses youtube-dl which has not been updated recently, it suffers from some issues (dislike_count not found...).
  • This repository is a simplified version of what pafy does, by just getting the url of the video and creating an OpenCV video capture from it.
  • It uses YT-DLP (https://github.com/yt-dlp/yt-dlp), which is a fork of youtube-dl that is updated frequently.

Requirement

  • YT-DLP
  • OpenCV
  • NumPy

Installation

  • The easiest way is to install it with pip:
pip install cap_from_youtube
  • You can also install it from GitHub:
pip install git+https://github.com/ibaiGorordo/cap_from_youtube

Usage

cap_from_youtube()

  • cap_from_youtube() is the main function to obtain a video capture from a YouTube video URL.
  • It requires the video URL as input and returns a cv2.VideoCapture object.
  • By default, it returns the video with the highest resolution available.
  • You can specify the resolution you want to get with the resolution parameter.
    • Available resolutions: '144p', '240p', '360p', '480p', '720p', '720p60', '1080p', '1080p60', 'best'
  • Example:
from datetime import timedelta
import cv2
from cap_from_youtube import cap_from_youtube

youtube_url = 'https://youtu.be/LXb3EKWsInQ'
start_time = timedelta(seconds=5)
cap = cap_from_youtube(youtube_url, 'best', start=start_time)

cv2.namedWindow('video', cv2.WINDOW_NORMAL)
while True:
    ret, frame = cap.read()
    if not ret:
        break
    cv2.imshow('video', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

list_video_streams()

  • You can also use the list_video_streams() function to get the list of available video streams.
  • It requires the video URL as input and returns two values:
    • streams: a list of VideoStream with the information of the available video streams.
    • resolutions: a NumPy array with the available resolutions.
  • Example:
from cap_from_youtube import list_video_streams

youtube_url = 'https://youtu.be/LXb3EKWsInQ'
streams, resolutions = list_video_streams(youtube_url)

for stream in streams:
    print(stream)

References

Release files for cap-from-youtube 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cap-from-youtube 0.2.0
File Size Uploaded
cap_from_youtube-0.2.0.tar.gz 4.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cap-from-youtube 0.2.0
File Interpreter ABI Platform
cap_from_youtube-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 8.8 kB

Release files / cap_from_youtube-0.2.0.tar.gz

Download URL cap_from_youtube-0.2.0.tar.gz
Size 4.1 kB
Tags Source
SHA-256 checksum
How to use checksums
6bd803caebac878c1a75183a43f55636f3b252e993e3a08e86fc7fb084be2f97
BLAKE2b-256 checksum
How to use checksums
47e4cfbd571ac083a010f3372543c9b8a6c0c4df074a139a3787c70d061a8d67
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.8

Release files / cap_from_youtube-0.2.0-py3-none-any.whl

Download URL cap_from_youtube-0.2.0-py3-none-any.whl
Size 4.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
38e6243310cb1e76b036ff82e8740781f7b492823ce5ad2a78f9ac470f16a22a
BLAKE2b-256 checksum
How to use checksums
cffbb7860bf3592025b04309986c2815d459945c11b2cad64a833fc57d303a9f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.8

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page