2017-05-11 17 views
1

私がしようとしている:動画をダウンロードするにはPythonスクリプト内pythonスクリプト内でyoutube-dlを使用して、再生リストから個々の動画URLを取得するにはどうすればよいですか?

  • 使用ユーチューブ-dlを定期的に
  • 整理/ユーチューブデータすなわち%(タイトル)■
  • 抽出オーディオ/によって動的動画に名前を付けますMP3とそれらのファイルを 'MP3'という名前のサブディレクトリに移動する

私はPythonにはかなり新しく、コードが不必要で不必要な部分があると確信しています。だから、私はあまりにもクリーンアップのアドバイスにも開いています。

個別のタイトルではなくプレイリストのURLを入力すると、ファイルを並べ替えるために使用したアップローダーのデータが表示されるという問題が発生しました。 (そして、私がコード全体でoutmplオプション/変数を使う方法、または使用できるかどうかわからない)

私は実際にコードを3つの部分/モジュールに分割しました。

問題の基本的な例がこれです - 私は次のように入力します

に動画を保存します
outmpl: 'F:\\Videos\\Online Videos\\Comics\\%(uploader)s\\%(playlist)s\\%(playlist_index)s_%(title)s.%(ext)s' 

'F:\\Videos\\Online Videos\\Comics\\Comicstorian\\Annhililation\\01_Annihilation Beginnings Drax Earthfall - Complete Story.mp4' - and so on (for the rest of the videos) 

しかし、私は上のディレクトリ変数を渡すために知っていませんファイルを移動するモジュールに転送します。ここ

コードは、 - 三つのモジュール/部品


PyFile_Download_Exmple.py

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 

ytdl_variables.py

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' 

Move_MP3

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) 

答えて

1

から個々のリンクを取得するには...

を私はもっとこれを掃除に取り組んでいますが、私は別の答えのセクションで答えを発見プレイリストURL:

ydl = youtube_dl.YoutubeDL({'outtmpl': '%(id)s%(ext)s', 'quiet':True,}) 
video = "" 

with ydl: 
    result = ydl.extract_info \ 
    (yt_url, 
    download=False) #We just want to extract the info 

    if 'entries' in result: 
     # Can be a playlist or a list of videos 
     video = result['entries'] 

     #loops entries to grab each video_url 
     for i, item in enumerate(video): 
      video = result['entries'][i] 

youtube_dl.YoutubeDLはJSONデータを返すようですYouTube API yt_urlは、動画または再生リストの変数です。

返されたデータが"entries"の場合、それはプレイリストなので、これらの各エントリ(i(ndex)のエントリを列挙します)をループします。そこからURLやその他の情報で必要なものを実行できます。

result['entries'][i]['webpage_url']  #url of video 
result['entries'][i]['title']   #title of video 
result['entries'][i]['uploader']  #username of uploader 
result['entries'][i]['playlist']  #name of the playlist 
result['entries'][i]['playlist_index'] #order number of video 
関連する問題