Search code examples
pythonyoutube-dl

How to get info about video, without downloading it, using youtube-dl


I made my own script using python and youtube-dl library to easily download songs from online radio. It works by copying a playlist, saving it to a .txt file, and then running a script that goes through the playlist and discards the time for each line, and then searches youtube with the song name and downloads the first one it finds. Since a lot of songs are repeated, the program downloads the song again and processes it (converts it to mp3 and isolates only the audio) and overwrites the old one. I’m interested in how to retrieve just the video name without downloading, and check if that song has already been downloaded to avoid wasting time and resources unnecessarily. I know that the "" team has songs ready, and I'm not sure if it can be used differently. Below is a .py script and an example of a song list (the list usually has 500 lines, so there is a lot of repetition).

<main.py>

import os
radio = "BBCRadio1"
lista = []
with open ("./list.txt" ,"r", encoding="utf8") as f:
    lines = f.readlines()
    for line in lines:
        lista.append(line.split("\t")[1].strip())
lista = list(dict.fromkeys(lista))
for song in lista:
    print("------------------------------------------Downloading " + str(lista.index(song) + 1) + ". song of " + str(len(lista)) + " songs------------------------------------------")
    os.system("youtube-dl  -o \"D:\Projects\YT-DL/songs/" + radio + "/%(title)s.%(ext)s\"  --extract-audio --audio-format mp3 \"ytsearch:" + song + "\"")
<BBCRadio1.txt>

14:32   Harry Styles - Watermelon Sugar
14:27   Drake - Passionfruit
14:24   Phoebe Bridgers - I See You
14:20   Avicii - Lonely Together (feat. Rita Ora)
14:17   Jawsh 685 & Jason Derulo - Savage Love (Laxed - Siren Beat)
14:13   Clipz, Ms. Dynamite, Ms Banks & JayKae - Again
14:09   Katy Perry - Chained To The Rhythm (feat. Skip Marley)
14:06   Dave - Funky Friday (Glastonbury 2019)
14:02   Dave - Location (Glastonbury 2019) (feat. Burna Boy)
13:58   Topic - Breaking Me (feat. A7S)
13:54   Lily Allen - The Fear
13:49   The Weeknd - I Feel It Coming (feat. Daft Punk)
13:46   Sports Team - Going Soft
13:42   Megan Thee Stallion & Beyoncé - Savage (Remix)
13:38   Jonas Brothers - Sucker

Solution

  • You can also use: yt_dlp it's a fork of youtube_dl, but downloads are very fast. Also in example bellow you will see the: hook function, so after completed download, you can process it as you want.

    Example bellow:

    import yt_dlp # pip install yt_dlp
    
    def hook(d):
        if d['status'] == 'finished':
            filename = d['filename']
            print(filename) # Here you will see the PATH where was saved.
    
    def client(video_url, download=False):
        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
             return ydl.extract_info(video_url, download=download)
    
    ydl_opts = { 
            'format': 'bestaudio/best',
            'outtmpl': '%(title)s.%(ext)s', # You can change the PATH as you want
            'download_archive': 'downloaded.txt',
            'noplaylist': True,   
            'quiet': True,
            'no_warnings': True,
            'postprocessors': [{
                'key': 'FFmpegExtractAudio',
                'preferredcodec': 'mp3',
                'preferredquality': '192',
            }],
            'progress_hooks': [hook]
    }
    

    now you can use your own list:

    for song in lista:
        song_details = client(f'ytsearch:{song}', download=False) # get the json details about the song  
        download     = client(f'ytsearch:{song}', download=True)  # download the song
    
        song_details['title']
        song_details['format_note']
        song_details['audio_ext']
        song_details['filesize']