Как скачать часть видео с YouTube

3 min read 7 hours ago
Published on Jan 24, 2025 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial will guide you through the process of downloading a specific segment of a YouTube video using two powerful tools: yt-dlp and ffmpeg. These tools allow you to extract and save just the part of the video you need, which is useful for creating clips, presentations, or simply saving highlights.

Step 1: Install yt-dlp and ffmpeg

Before you can download a segment of a video, you need to have both yt-dlp and ffmpeg installed on your system.

Installing yt-dlp

  1. Visit the yt-dlp GitHub page: yt-dlp GitHub.
  2. Follow the installation instructions for your operating system. Common methods include using pip:
    pip install yt-dlp
    

Installing ffmpeg

  1. Visit the ffmpeg builds page: FFmpeg Builds.
  2. Download the appropriate version for your operating system.
  3. Follow the installation instructions provided on the website.

Step 2: Get the Video URL

You need the URL of the YouTube video you want to download a segment from.

  1. Go to the YouTube video page.
  2. Copy the URL from your browser’s address bar.

Step 3: Extract Video Stream URL

Using yt-dlp, you will extract the direct video stream URL.

  1. Open your command-line interface.
  2. Run the following command, replacing %URL% with the actual video URL:
    yt-dlp -g %URL%
    
  3. This command will return the direct URL of the video stream. Copy this URL for the next step.

Step 4: Download the Video Segment

Now, use ffmpeg to download the specific segment of the video.

  1. In your command-line interface, run the following command:
    ffmpeg -hwaccel auto -i %URL1% -i %URL2% -map 0:0 -map 1:0 -c copy -ss 00:00:15 -to 00:00:30 -avoid_negative_ts make_zero -f matroska -y output.mkv
    
    • Replace %URL1% with the video stream URL you obtained in Step 3.
    • Replace %URL2% with any additional input URL or leave it as is for a single stream.
    • Adjust the -ss and -to parameters to specify the start and end times of the segment you want to download (in this case, from 15 seconds to 30 seconds).
    • The output file will be saved as output.mkv.

Conclusion

You have now successfully downloaded a segment of a YouTube video using yt-dlp and ffmpeg. You can modify the start and end times as needed to capture different segments. This method is efficient for creating clips for personal use or sharing highlights from videos. For further exploration, consider experimenting with other ffmpeg options for different output formats or additional processing.