0

This is a code submitted by user Pedro Lobito a year ago here: https://stackoverflow.com/a/60490761/874188

It should download a youtube .mp3 file based on the keywords searched.

import youtube_dl  # youtube-dl-2020.3.1
import traceback, os, json
from youtube_search import YoutubeSearch  # pip install youtube_search 

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")

As you can test, for the example provided under search string, it works perfectly fine. However, let's say if you put something like 'as we fall' keywords for the youtube video, os.path.isfile will get marked as False and downloading won't work. It appears to work 50/50, some keywords work, others not, and I have absolutely no idea why os.path.isfile behaves like that. I tried to get in contact with original poster but my rep is too low. Original post:how to use a key words instead of a url with youtube-dl and discord.py?

EDIT 1 The reason why os.path.isfile sometimes returns false for seemingly no reason is because the youtube titles sometimes may have characters forbidden in file names. Could this problem be somewhat fixable or is there any alternative to bypass this? My os is windows 10.

EDIT 2 Well, I'm screwed. https://superuser.com/questions/1112132/how-to-create-folder-name-or-file-name-with-special-characters-like

tripleee
  • 175,061
  • 34
  • 275
  • 318
B44
  • 37
  • 4
  • Probably the failing titles contain characters which are not allowed in file names by your OS, or perhaps the code contains invalid assumptions about the expected file name. Doesn't the `info` object contain the actual file name? – tripleee Feb 16 '21 at 09:12
  • after looking after your suggestions, I've found that the as we fall song indeed has forbidden character | for file names. So, It's safe to say that is the actual problem. But the question is how should i fix it? – B44 Feb 16 '21 at 09:14
  • 1
    The trivial fix is probably to not use Windows. You can thank me later. – tripleee Feb 16 '21 at 09:15
  • Yeah right..and as for the non-trivial solutions, I suppose os module doesn't have anything built in that could handle this problem? – B44 Feb 16 '21 at 09:19
  • @B44 could you please share an example of title which contains these so called forbidden characters? Curious. Thanks – Harsh Feb 16 '21 at 09:23
  • For example, the file downloaded was: As We Fall | Varus Music Video - League of Legends-vzNcSvKCOyA.mp3 But, since | is forbidden in windows, it got saved as : As We Fall _ Varus Music Video - League of Legends-vzNcSvKCOyA.mp3 and os.path.isfile returned Fasle appropriately. – B44 Feb 16 '21 at 09:25
  • For what it's worth, the code by Pedro has a number of issues. It should not have a blanket `except` and it should probably check the result code from the library rather than examine whether the file actually ended up on disk. – tripleee Feb 16 '21 at 10:04
  • Ok, so it does download files, but changes name to remove bad characters. This is where `os.path.isfile` fails, but the module itself is not at fault. Now, if the only objective was to check whether download succeeded, like @tripleee mentioned, `download_retcode` should be checked, and it could help. Alternatively, you could search for files which contain youtube id string in it to confirm file download, because ids are supposed to be unique(given you don't do multiple downloads with other options of same file), and these youtube ones look simpler especially since they are part of the url. – Harsh Feb 16 '21 at 10:50
  • Disappointingly, it looks like `youtube_dl` in fact does not expose the filename of the downloaded file (in a place where I could discover it in about 15 minutes of reading the code). My proposed fix would still be to just take out the silly check, and probably the `try`/`except` wrapper too. – tripleee Feb 16 '21 at 11:39

0 Answers0