Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在youtube dl和discord.py中使用关键字而不是url?_Python_Discord.py_Youtube Dl - Fatal编程技术网

Python 如何在youtube dl和discord.py中使用关键字而不是url?

Python 如何在youtube dl和discord.py中使用关键字而不是url?,python,discord.py,youtube-dl,Python,Discord.py,Youtube Dl,我正在编写一个通用机器人,基本上我想键入命令,然后键入歌曲名称示例:?播放歌曲名称,它将搜索youtube,弹出的第一个视频将下载其音频 我让机器人与正常的链接,但如果我必须得到的链接播放音乐,它失败的目的 client = discord.Client() @client.event async def on_message(message): ydl_opts = { 'format': 'beataudio/best', 'postp

我正在编写一个通用机器人,基本上我想键入命令,然后键入歌曲名称示例:
?播放歌曲名称
,它将搜索youtube,弹出的第一个视频将下载其音频

我让机器人与正常的链接,但如果我必须得到的链接播放音乐,它失败的目的

client = discord.Client()
@client.event
async def on_message(message):
    ydl_opts = {
            'format': 'beataudio/best',
            'postprocessors': [{
                'key': 'FFmpegExtractAudio',
                'preferredcodec': 'mp3',
                'preferredquality': '192'
            }]
        }
     with youtube_dl.YoutubeDL(ydl_opts) as ydl:
          print("Downloading audio now\n")
          url: str = message.content.replace('?play ', '')
          print(url)
          ydl.download([url])

我以前没有使用过youtube dl,所以我不知道它是如何工作的。

一旦您得到discord搜索查询,您可以使用:

import youtube_dl  # youtube-dl-2020.3.1
import traceback, os, json
from youtube_search import YoutubeSearch  # pip install youtube_search 
"""
sources :
https://github.com/ytdl-org/youtube-dl/blob/master/README.md#embedding-youtube-dl
https://stackoverflow.com/questions/23727943/how-to-get-information-from-youtube-dl-in-python/31184514#31184514
https://stackoverflow.com/a/43143553/797495
"""

search = 'carlos paiao playback'
ydl_opts = {
    'format': 'beataudio/best',
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'mp3',
        'preferredquality': '192'
    }]
}
yt = YoutubeSearch(search, max_results=1).to_json()
try:
    yt_id = str(json.loads(yt)['videos'][0]['id'])
    yt_url = 'https://www.youtube.com/watch?v='+yt_id
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download([yt_url])
        info = ydl.extract_info(yt_url)
        songname = info.get('title', None) + "-" + yt_id + ".mp3"
        if os.path.isfile(songname):
            print("Song Downloaded: " + songname)
        else:
            print("Error: " + songname)
except:
    pass
    print(traceback.print_exc())
    print("no results")

获得discord搜索查询后,可以使用:

import youtube_dl  # youtube-dl-2020.3.1
import traceback, os, json
from youtube_search import YoutubeSearch  # pip install youtube_search 
"""
sources :
https://github.com/ytdl-org/youtube-dl/blob/master/README.md#embedding-youtube-dl
https://stackoverflow.com/questions/23727943/how-to-get-information-from-youtube-dl-in-python/31184514#31184514
https://stackoverflow.com/a/43143553/797495
"""

search = 'carlos paiao playback'
ydl_opts = {
    'format': 'beataudio/best',
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'mp3',
        'preferredquality': '192'
    }]
}
yt = YoutubeSearch(search, max_results=1).to_json()
try:
    yt_id = str(json.loads(yt)['videos'][0]['id'])
    yt_url = 'https://www.youtube.com/watch?v='+yt_id
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download([yt_url])
        info = ydl.extract_info(yt_url)
        songname = info.get('title', None) + "-" + yt_id + ".mp3"
        if os.path.isfile(songname):
            print("Song Downloaded: " + songname)
        else:
            print("Error: " + songname)
except:
    pass
    print(traceback.print_exc())
    print("no results")

没有找到我要找的东西@ZacharyCraigdid没有找到我要找的东西@扎查里克雷格