Search code examples
pythonyoutube-dl

Youtube-DL Search Return URL of Video


So I want to search a Youtube Video with the ytsearch function of youtube-dl and give out the url of the found video and store it in a variable. Is there an option to do that in Python?

As an example, how would I get the url from the code below?

async def play(ctx):
args = ctx.message.content.split(" ")
os.system('youtube-dl --get-url "ytsearch:{}"'.format(args[1]))

Solution

  • I'm not sure if this will work but if you are running windows you can add > filename.txt to the end of your os argument to make an output of the cmd command.

    async def play(ctx):
    args = ctx.message.content.split(" ")
    os.system('youtube-dl --get-url "ytsearch:{}"'.format(args[1])+ "> filename.txt")
    

    Then you can read the file and store that result in a variable by writing...

    file = open("filename.txt","r")
    output = file.read()
    file.close()
    

    Im assuming that whatever the output of the command is would be the url?