Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

python - Downloading a YouTube clip at 1080p from a list of links

Problem Statement: Downloading a certain clip of a YouTube video at 1080p from a list of YouTube links

So I tried the PyTube library but it doesn't seem to support downloading a certain segment of the video. There has been a question about the same, and the answer to that supports post-processing in the video to clip it out, but I want to download only that specific clip and not the entire video. Although I am looking for an option that supports downloading multiple videos from a set of links. Below is the code I used for the same.

import time
import pytube
import os
from pytube import YouTube

YouTubeIDs = ['QC8iQqtG0hg']
StartSeg = [2]
EndSeg = [3]

for i in range(len(YouTubeIDs)):
  print(f'Downloading Video {i} with ID {YouTubeIDs[i]}')
  ID = YouTubeIDs[i]
  StartSegment = StartSeg[i]
  EndSegment = EndSeg[i]
  YTLink = f'https://www.youtube.com/embed/{ID}?start={StartSegment}&end={EndSegment}'
  print(f'Link to the YouTube Video - {YTLink}')
  time.sleep(2)
  yt = YouTube(YTLink)
  yt.streams.filter(res="720p").first().download()
  os.rename(yt.streams.filter(res="720p").first().download(), f'YouTube_{i}.mp4')
  print(f'Finished Downloading Video {i}, renamed as YouTube_{i}.mp4')

Another approach was tried using youtube-dl, but it didn't work because the downloaded video was of 0 KBs. I also tried in a python environment even after the os import, but it gave the same error and I'd very much prefer a Python implementation and not use the bash script unless very necessary.

%%bash

ffmpeg $(youtube-dl -g 'https://www.youtube.com/watch?v=NnW5EjwtE2U' | sed "s/.*/-ss 10 -i &/") -t 60 -c copy test3.mp4

Sidenote: The idea is to be able to download a specific segment (of about 5-6 seconds) of YouTube video from a CSV file of approximately 2.6 million links. The CSV file contains columns like ['YouTube ID', 'start segment', 'end segment']

Would be grateful to get help in the same. Please let me know if I need to clarify it more. Thanks.

question from:https://stackoverflow.com/questions/65859976/downloading-a-youtube-clip-at-1080p-from-a-list-of-links

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I have something different for you but i think it will help you! pip install pytube

from pytube import YouTube
yt = YouTube("https://www.youtube.com/watch?v=n06H7OcPd-g")
yt = yt.get('mp4', '720p')
yt.download('/path/to/download/directory')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...