Spaces:
Running

README / dl.py
tesneeem's picture
Upload 8 files
14dabf7 verified
Raw History Blame Contribute Delete
3.51 kB
from yt_dlp import YoutubeDL
import re
import os
import threading
def dlnone(url):
if os.path.exists(remove_invalid_path_chars(url)+".mp3"):
return url
print("DL",url)
ydl_opts = {'format': 'bestaudio/best','postprocessors': [{'key': 'FFmpegExtractAudio','preferredcodec': 'mp3'}],'outtmpl':remove_invalid_path_chars(url)}
with YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
def dl(url,wait):#URLをdlする
if wait=="wait":#初回のみここでDL
if os.path.exists(remove_invalid_path_chars(url)+".mp3"):
return url
print("DL",url)
ydl_opts = {'format': 'bestaudio/best','postprocessors': [{'key': 'FFmpegExtractAudio','preferredcodec': 'mp3'}],'outtmpl':remove_invalid_path_chars(url)}
with YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
return url
else:#以後は非定義同期
thread = threading.Thread(target=dlnone, args=(url))
thread.start
def urlinfo(url):#urlの情報を出力する
info = YoutubeDL().extract_info(url, download=False)
title = info["title"]
service = info["extractor"]
return title,service
def remove_invalid_path_chars(input_str):
# 使用できない文字の正規表現パターンを定義します
invalid_chars_pattern = r'[\\/:*?"<>|]'
# 正規表現パターンに一致する文字を削除して、新しい文字列を作成します
cleaned_str = re.sub(invalid_chars_pattern, '', input_str)
return cleaned_str
def get_playlist_info(playlist_url):
ydl_opts = {
'quiet': True,
'extract_flat': True,
'force_generic_extractor': True,
'skip_download': True,
'dump_single_json': True,
}
with YoutubeDL(ydl_opts) as ydl:
result = ydl.extract_info(playlist_url, download=False)
if 'entries' in result:
playlist_name = result['title']
n =0
listn = count_videos_in_playlist(playlist_url)+1
while True:
if n==listn:
break
try:
a =get_video_info(get_first_video_url(playlist_url,n))[1]
break
except:
n=n+1
if n==listn:
return "Error","None"
return playlist_name,a
def get_video_info(video_url):
ydl_opts = {
'quiet': True,
'skip_download': True,
'dump_single_json': True,
}
with YoutubeDL(ydl_opts) as ydl:
result = ydl.extract_info(video_url, download=False)
if 'title' in result:
video_title = result['title']
video_thumbnail = result['thumbnails'][0]['url']
return video_title, video_thumbnail
def get_first_video_url(playlist_url,n):
ydl_opts = {
'quiet': True,
'extract_flat': True,
}
with YoutubeDL(ydl_opts) as ydl:
playlist_info = ydl.extract_info(playlist_url, download=False)
if 'entries' in playlist_info:
first_video_info = playlist_info['entries'][n]
first_video_url = first_video_info['url']
return first_video_url
def count_videos_in_playlist(playlist_url):
ydl_opts = {
'extract_flat': 'in_playlist',
'quiet': True,
}
with YoutubeDL(ydl_opts) as ydl:
result = ydl.extract_info(playlist_url, download=False)
if 'entries' in result:
video_count = len(result['entries'])
return video_count
else:
return 0