def determine_finame_from_tag(filename): """ determine final filename from metatags """ print(my_colored("filename conversion with metadata", "green")) nom_genere = "" if not is_ffmpeg_installed() or is_m4a(): print( my_colored( "[warning] If you want use metadata tags, install ffmpeg and use MP3 or OGG format.", "yellow", )) print(my_colored("[warning] keep the YouTube format.", "yellow")) return filename title, artist, album = obtain_tags(filename) if not title or not artist: print(my_colored("[warning] Not enough tags information", "yellow")) return filename if album: nom_genere = (unicode_and_trim(artist) + " - " + unicode_and_trim(album) + " - " + unicode_and_trim(title)) else: nom_genere = (unicode_and_trim(artist) + " - " + unicode_and_trim(title)) print_debug("file name deduced from metadata : ") return find_unique_name(nom_genere + extension(filename))
def update_pip_package(prog, package): """ update pip 'package' with 'prog' """ try: print_try_update(package, prog) params = [ prog, "install", "--upgrade", package, ] print_debug("full trace pip") output = subprocess.check_output(params) # print(output) for line in output.decode("utf-8").split("\n"): if is_verbose(): print(line.strip(" ")) elif "Successfully" in line: print(line.strip(" ")) elif ("Requirement already satisfied: " + package) in line or ( "Requirement already up-to-date: " + package) in line: print(line.strip(" ")) print("Update ok") return True except Exception: print_error_update_package() return False
def unicode_and_trim(text_to_translate): """ transcode text to unicode, without special characters, and trim spaces before after """ print_debug("raw format : " + text_to_translate) step_1 = re.sub("(\\W+)", " ", text_to_translate) step_2 = re.sub(" +", " ", step_1) step_3 = re.sub("^ ", "", step_2) step_4 = re.sub(" $", "", step_3) transcode_text = unidecode.unidecode(step_4) print_debug("unicode format : " + transcode_text) return transcode_text
def search(keywords): """ search the items with youtube-search-python return a json with 5 entries of YouTube results param : the artist and the song """ print_debug("search : " + keywords) print( my_colored_emoji( "\U0001F50E", "search " + keywords + '" with youtube-search-python', "green", )) my_limit = int(param_number()) return VideosSearch(keywords, limit=my_limit)
def batch(params): """ batch job """ file_path = params[0] has_header = params[1] separator = params[2] search = "" with open(file_path, "r", encoding="utf-8") as csvfile: reader = csv.reader(csvfile, delimiter=separator, quotechar="|") if has_header == "True": next(reader, None) for row in reader: search = "" if is_verbose(): print_debug(str(reader.line_num) + " en cours : " + str(row)) for element in str(params[3]).split("+"): search = search + " " + row[int(element) - 1] job(search.replace(" ", "", 1))
def find_unique_name(filename): """ determine final filename to escape already existing filename """ print_debug("initial filename found : " + filename) if os.path.exists(filename): # loop to find non existent filename print_debug(filename + " already exists") i = 0 while True: i += 1 tmp = (name_without_extension(filename) + "_" + str(i) + extension(filename)) if not os.path.exists(tmp): filename = tmp break print_debug(tmp + " already exists") print_debug("final available filename found : " + filename) return filename
def determine_filename(search, title): """ determine inital filename """ if is_m4a() or not is_ffmpeg_installed(): ext = ".m4a" elif is_ogg(): ext = ".ogg" else: ext = ".mp3" print_debug("extension used : " + ext) if is_keep(): print_debug("file name will be deduced from YouTube : ") return find_unique_name(unicode_and_trim(title) + ext) if is_tag(): print_debug("temporary file name will be deduced from YouTube : ") return find_unique_name(unicode_and_trim(title) + ext) print_debug("file name will be deduced key words : ") return find_unique_name(unicode_and_trim(search) + ext)
def download_song(song_url, filename): """ download song with yt-dlp in filename from url song_url """ print( my_colored_emoji("\U0001F4BE", "download " + song_url + " with yt_dlp", "green") ) # m4a opts = { "outtmpl": name_without_extension(filename) + ".%(ext)s", "format": "m4a/best", } if extension(filename) == ".mp3": opts["format"] = "bestaudio/best" opts["postprocessors"] = [ { "key": "FFmpegExtractAudio", "preferredcodec": "mp3", "preferredquality": "256", }, {"key": "FFmpegMetadata"}, ] if is_quality(): opts.get("postprocessors")[0]["preferredquality"] = "320" if extension(filename) == ".ogg": opts["format"] = "bestaudio/best" opts["postprocessors"] = [ { "key": "FFmpegExtractAudio", "preferredcodec": "vorbis", }, {"key": "FFmpegMetadata"}, ] if is_verbose(): opts["verbose"] = "True" print_debug("debug yt-dlp is activated") if not is_quiet(): print("start yt-dlp operation") elif is_quiet(): opts["quiet"] = True opts["no_warnings"] = True with yt_dlp.YoutubeDL(opts) as ydl: ydl.extract_info(song_url, download=True) if not is_quiet(): print("end yt-dlp operation") if not is_ffmpeg_installed() and not is_m4a() and not is_ogg(): print( my_colored( "[warning] If you want MP3/OGG format, install ffmpeg.", "yellow", ) ) print( my_colored( "[warning] To disable this message activate -f", "yellow", ) )
is_verbose, is_quiet, is_m4a, is_ogg, is_quality, my_colored, my_colored_emoji, ) from ytdlmusic.file import extension from ytdlmusic.file import name_without_extension, is_ffmpeg_installed from ytdlmusic.log import print_debug try: import yt_dlp except ImportError: print_debug("yt_dlp import problem") def download_song(song_url, filename): """ download song with yt-dlp in filename from url song_url """ print( my_colored_emoji("\U0001F4BE", "download " + song_url + " with yt_dlp", "green") ) # m4a opts = {
""" search utils scripts """ from ytdlmusic.log import print_debug from ytdlmusic.params import param_number, my_colored_emoji try: from youtubesearchpython import VideosSearch except ImportError: print_debug("youtubesearchpython import problem") def search(keywords): """ search the items with youtube-search-python return a json with 5 entries of YouTube results param : the artist and the song """ print_debug("search : " + keywords) print( my_colored_emoji( "\U0001F50E", "search " + keywords + '" with youtube-search-python', "green", )) my_limit = int(param_number()) return VideosSearch(keywords, limit=my_limit)