import yt_dlp import time import subprocess txt_file = "michael.txt" def download_song(url, track_id, output_dir='michael_tracks'): subprocess.run(["yt-dlp", '--default-search', 'ytsearch', url, "--output", f"{output_dir}/%(title)s.%(ext)s", "--embed-thumbnail", "--extract-audio"]) def download_all(txt_file): downloaded = [] final_file = ".".join(txt_file.split(".")[:-1]) + "_final.txt" with open(txt_file, "r") as txt: with open(final_file, "w") as final: lines = txt.readlines() print("hi") last_track = "" last_time = "" ids = 1 for l in lines: timestamp = l.split("\t")[0] track_name = ")".join(l.split(")")[1:])[:-1] print(track_name) if track_name == last_track and track_name not in downloaded: downloaded.append(track_name) print("Downloading track id " + str(ids) + f" with timestamp {last_time} and name '{track_name}'") download_song(f"ytsearch:{track_name}", ids) final.write(str(ids) + "\t" + last_time + "\t" + track_name) final.flush() ids += 1 last_track = track_name last_time = timestamp final.close() txt.close() download_all(txt_file) # Example usage #download_song('https://www.youtube.com/watch?v=dQw4w9WgXcQ')