请求协助编写脚本:从YouTube直播每小时截取帧制作延时视频
Hey there! I’ve tackled this exact task a few times, so let’s walk through a reliable, step-by-step workflow using two industry-standard tools: yt-dlp (for accessing live streams) and ffmpeg (for frame extraction and timelapse compilation). They’re cross-platform, lightweight, and perfect for this job.
Step 1: Install Required Tools
First, make sure you have both tools set up on your system:
- yt-dlp: A robust fork of youtube-dl that handles live streams seamlessly.
- On Ubuntu/Debian:
sudo apt install yt-dlp - On macOS (with Homebrew):
brew install yt-dlp - On Windows: Download the executable (no installation needed—just place it in your system PATH)
- On Ubuntu/Debian:
- ffmpeg: The go-to tool for all video processing tasks.
- On Ubuntu/Debian:
sudo apt install ffmpeg - On macOS:
brew install ffmpeg - On Windows: Grab a pre-built package and add it to your PATH
- On Ubuntu/Debian:
Step 2: Get the Direct Live Stream URL
YouTube doesn’t expose direct download links for live streams, so we’ll use yt-dlp to fetch the underlying stream URL:
yt-dlp -g "https://www.youtube.com/watch?v=YOUR_LIVE_STREAM_ID"
This will output two URLs (one for video, one for audio). Copy the first one—we only need the video stream for frame extraction.
Step 3: Extract One Frame Per Hour
First, create a folder to store your frames:
mkdir frames
Then run this ffmpeg command to capture a frame every hour from the stream:
ffmpeg -i "PASTE_YOUR_STREAM_URL_HERE" -vf "fps=1/3600" -q:v 2 frames/frame_%04d.jpg
Let’s break down what each part does:
-i: Points to the live stream URL we copied earlier-vf "fps=1/3600": Sets the capture rate to 1 frame every 3600 seconds (1 hour)-q:v 2: Controls JPEG quality (0 = highest, 31 = lowest; 2 strikes a great balance of quality and file size)frames/frame_%04d.jpg: Saves frames to theframesfolder with sequential numbering (e.g., frame_0001.jpg, frame_0002.jpg)
Leave this command running as long as the live stream is active—it’ll keep capturing frames automatically.
Step 4: Stitch Frames into a Timelapse Video
Once you have all your frames, use ffmpeg to turn them into a smooth timelapse. This example creates a standard 24fps video:
ffmpeg -framerate 24 -i frames/frame_%04d.jpg -c:v libx264 -r 24 -pix_fmt yuv420p timelapse.mp4
Key parameters explained:
-framerate 24: Tells ffmpeg to use 24 input frames per second of video. At this rate, each second of your timelapse will cover 24 hours of the live stream. Adjust this for faster/slower playback (e.g.,-framerate 12= 12 hours per second)-c:v libx264: Uses the H.264 codec for high-quality, widely compatible video-pix_fmt yuv420p: Ensures the video plays on all devices (including older ones)
Pro Tips & Troubleshooting
- Disk Space Check: If the stream runs for days, each high-quality JPEG frame is ~1-2MB—make sure you have enough storage.
- Update yt-dlp: YouTube changes its stream format often, so run
yt-dlp -Uto keep the tool updated if you hit errors. - Handle Completed Streams: If the live stream has already ended, use the regular video URL instead of the stream URL—all commands still work.
- Skip Unwanted Sections: Add
-ss HH:MM:SSto the frame extraction command to skip the start of the stream (e.g.,-ss 01:00:00skips the first hour).
If you run into any snags or need to tweak the workflow for your specific needs, feel free to ask!
内容的提问来源于stack exchange,提问作者Francesco

