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

Add functionality to download specific segments of a YouTube video #1912

Open
racoci opened this issue Apr 16, 2024 · 2 comments
Open

Add functionality to download specific segments of a YouTube video #1912

racoci opened this issue Apr 16, 2024 · 2 comments

Comments

@racoci
Copy link

racoci commented Apr 16, 2024

Is your feature request related to a problem? Please describe.
Yes, the current limitation is the inability to download specific segments of a YouTube video directly using pytube. Users often need only a particular part of a video for various purposes, such as creating highlight reels, educational content, or analysis. Downloading entire videos is not only inefficient in terms of bandwidth and storage but also time-consuming when processing the video afterwards to extract the needed segment.

Describe the solution you'd like
I would like pytube to include an option to specify start and end times for the video download process. This feature would allow users to download only the desired portion of a video directly, without the need for post-download editing. Ideally, this could be implemented as additional parameters in the existing download function or as a new method specifically for partial downloads.

Describe alternatives you've considered
An alternative solution is to download the entire video and then use a separate tool like ffmpeg to trim the video to the desired segment. However, this is a two-step process that requires additional time and resources. Another alternative is to use different libraries or tools that support partial downloads, but they may not be as user-friendly or well-maintained as pytube.

Additional context
This feature would significantly enhance the usability of pytube for a wide range of users, including content creators, educators, and researchers, who often work with video data. It aligns with the growing need for efficient data usage and streamlined workflows in video processing tasks.

I don't know if it's viable to do this here (or integrate to avoid dependencies), but I tested using yt_dlp and it worked:

import os
import sys
from yt_dlp import YoutubeDL

def download_video_segment(video_url, start_time, end_time, output_file_name):
    # Convert start and end times from seconds to hh:mm:ss format
    start_time_hms = str(int(start_time) // 3600).zfill(2) + ':' + str((int(start_time) % 3600) // 60).zfill(2) + ':' + str(int(start_time) % 60).zfill(2)
    end_time_hms = str(int(end_time) // 3600).zfill(2) + ':' + str((int(end_time) % 3600) // 60).zfill(2) + ':' + str(int(end_time) % 60).zfill(2)

    # Set up yt-dlp options
    ydl_opts = {
        'format': 'bestvideo+bestaudio',
        'external_downloader': 'ffmpeg',
        'external_downloader_args': ['-ss', start_time_hms, '-to', end_time_hms],
        'outtmpl': output_file_name,
    }

    # Download the video using yt-dlp
    with YoutubeDL(ydl_opts) as ydl:
        ydl.download([video_url])

if __name__ == "__main__":
    if len(sys.argv) != 5:
        print("Usage: python script.py VIDEO_URL START_TIME_SECONDS END_TIME_SECONDS OUTPUT_FILE_NAME")
        sys.exit(1)

    # Command line arguments
    video_url = sys.argv[1]
    start_time_seconds = sys.argv[2]
    end_time_seconds = sys.argv[3]
    output_file_name = sys.argv[4]

    download_video_segment(video_url, start_time_seconds, end_time_seconds, output_file_name)
Copy link

Thank you for contributing to PyTube. Please remember to reference Contributing.md

@racoci racoci changed the title Clipping: Add functionality to download specific segments of a YouTube video Add functionality to download specific segments of a YouTube video Apr 16, 2024
@pedrosalomaodw
Copy link

nice

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

No branches or pull requests

2 participants