Wie rufe ich einzelne Video-URLs aus einer Wiedergabeliste ab und verwende YouTube-DL in einem Python-Skript?
Posted: 25 Apr 2025, 09:40
Ich versuche: < /p>
Verwenden Sie YouTube-DL innerhalb eines Python-Skripts, um Videos regelmäßig herunterzuladen. 'Mp3' < /li>
< /ul>
Ich bin ziemlich neu in Python und ich bin sicher, dass es einige unordentliche, unnötige Code -Teile gibt. Ich bin also auch offen für Aufräumberatung. (Und ich weiß nicht, wie oder ob ich im gesamten Code nur die OutMPL -Option /Variable verwenden kann) < /p>
Ich habe den Code tatsächlich in drei Teile /Module aufgeteilt.
Verwenden Sie YouTube-DL innerhalb eines Python-Skripts, um Videos regelmäßig herunterzuladen. 'Mp3' < /li>
< /ul>
Ich bin ziemlich neu in Python und ich bin sicher, dass es einige unordentliche, unnötige Code -Teile gibt. Ich bin also auch offen für Aufräumberatung. (Und ich weiß nicht, wie oder ob ich im gesamten Code nur die OutMPL -Option /Variable verwenden kann) < /p>
Ich habe den Code tatsächlich in drei Teile /Module aufgeteilt.
Code: Select all
outmpl: 'F:\\Videos\\Online Videos\\Comics\\%(uploader)s\\%(playlist)s\\%(playlist_index)s_%(title)s.%(ext)s'
< /code>
, wodurch die Videos in: < /p>
speichern würden'F:\\Videos\\Online Videos\\Comics\\Comicstorian\\Annhililation\\01_Annihilation Beginnings Drax Earthfall - Complete Story.mp4' - and so on (for the rest of the videos)
< /code>
Aber ich weiß nicht, dass ich die Verzeichnisvariablen an das Modul weitergeben soll, in dem ich die Dateien verschiebe. />
[b]pyFile_download_exmple.py[/b]
from __future__ import unicode_literals
import youtube_dl
import Move_MP3
import ytdl_variables
#Uses variables from ytdl_variables script and downloads the video
with youtube_dl.YoutubeDL(ytdl_variables.ydl_opts) as ydl:
ydl.download([ytdl_variables.video_url])
#Calls script to create folder and move MP3 files
Move_MP3
< /code>
[b]Ytdl_variables.py[/b]
from __future__ import unicode_literals
import youtube_dl
global video_title, uploader, playlist, playlist_index, video_url, ydl_opts, ydl
video_url = 'https://www.youtube.com/playlist?list=PL6FhCd_HO_ACJzTiLKfETgzLc1an_t05i'
ydl_opts = {
'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
'outtmpl': 'F:\\Videos\\Online Videos\\Comics\\%(uploader)s\\%(playlist)s\\%(playlist_index)s_%(title)s.%(ext)s',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
'download_archive': 'F:\\Videos\\Online Videos\\Archive.txt',
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
#The next part creates a variable that returns info when provided the video_url variable >> http://stackoverflow.com/questions/23727943/how-to-get-information-from-youtube-dl-in-python
'''
Code here should get take the youtube playlist and spit out
each url to move to the next step as vLinks variable, but
I haven't figured out how to pass (title) etc. variables from
each video in a playlist.
link = individual video url from playlist
The following puts actual info into variables for Python to use. These are made global above. I made a 'for' loop to repeat grabbing info for each video - but it doesn't work right now b/c I didn't define vLinks.
'''
for vLink in vLinks:
info_dict = ydl.extract_info(link, download=False)
video_title = info_dict.get('title', None)
playlist_index = info_dict.get('playlist_index', None)
playlist = info_dict.get('playlist', None)
uploader = info_dict.get('uploader', None)
print(video_title)
#Checks if the video is in a playlist; if it's not, 'NA' will be the string returned: http://stackoverflow.com/questions/23086383/how-to-test-nonetype-in-python
if playlist is None:
playlist = 'NA'
if playlist_index is None:
playlist_index = 'NA'
< /code>
Move_mp3 < /strong> < /p>
from __future__ import unicode_literals
import ytdl_variables
import shutil
import os, os.path
#Sets variables for renaming the files
newfolder = 'F:\\Videos\\Online Videos\\Comics\\' + ytdl_variables.uploader + '\\' + ytdl_variables.playlist + '\\MP3\\'
oa_savedir = 'F:\\Videos\\Online Videos\\Comics\\' + ytdl_variables.uploader + '\\' + ytdl_variables.playlist + '\\' + ytdl_variables.playlist_index + '_' + ytdl_variables.video_title + '.mp3'
fa_savedir = 'F:\\Videos\\Online Videos\\Comics\\' + ytdl_variables.uploader + '\\' + ytdl_variables.playlist + '\\MP3\\' + ytdl_variables.playlist_index + '_' + ytdl_variables.video_title + '.mp3'
#Function that creates file directory before moving file there - changed from http://stackoverflow.com/questions/23793987/python-write-file-to-directory-doesnt-exist
def mkdir_p(path):
if not os.path.exists(path):
os.makedirs(path);
#Function that checks whether the file already exists where I want to move it >> http://stackabuse.com/python-check-if-a-file-or-directory-exists/
def chkfl_p(path):
if not os.path.isfile(path):
shutil.move(oa_savedir, fa_savedir);
#Calls function to look for \MP3 directory and creates directory if it doesn't exist
mkdir_p(newfolder)
#Calls function to look for file and moves file if it isn't already there
chkfl_p(fa_savedir)