How to change the title of audio before downloading in yt-dlp

255 views Asked by At

Metadata of any song which has '|' in title is not being extracted so i want to change '|' to '-'and then download but it doesn't change.

When i run this code it does download audio but doesn't change | to -.

import yt_dlp
from mutagen.easyid3 import EasyID3
import os

def download_and_tag_audio():
    url = input("Enter audio url: ")
    genre = input("Genre of this music:\t")
    directory = os.path.expanduser('~/Music/')
    ytdl_options = {
        'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '320',
                    }],
        'outtmpl': os.path.join(directory, '%(title)s.%(ext)s'),
    }
    ytdl = yt_dlp.YoutubeDL(ytdl_options) 
    info = ytdl.extract_info(url, download=False)
        
    if info is None:
            print("Error 404!")
    else:
        # Replace '|' character in title
        info['title'] = info['title'].replace('|', '-')        
        # Download audio
        ytdl.download(url)
        audio_file_path = ('/home/unish/Music/'+info['title']+'.mp3')
        metatag = EasyID3(audio_file_path)
        metatag['title'] = info['title']
        metatag['artist'] = info['uploader']
        metatag['genre'] = genre
        metatag.save()
    print("Download Completed.")

download_and_tag_audio()
0

There are 0 answers