yt-dlp

yt-dlp - UnicodeEncodeError


I get the error below on my windows 10 when I try to extract audio that has none ASCII characters using yt-dlp.

return codecs.charmap_encode(input,self.errors,encoding_table)[0]
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'charmap' codec can't encode characters in position 143-147: character maps to <undefined>

To fix the issue, I have tried to restrict the filename using options below, but none of it works so far.

ydl_opts = {
    'restrict-filenames': True, 
    'restrictfilenames': True, 
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
    try:
        info = ydl.extract_info("ytsearch:%s" % requestedAudio, download=False)
    except yt_dlp.utils.DownloadError or yt_dlp.utils.ExtractorError:
        // do more.. 

How can I fix this? Thank you.


Solution

  • I think you can try setting the PYTHONIOENCODING environment variable to utf-8 before running your script

    check this out :

    import os
    os.environ['PYTHONIOENCODING'] = 'utf-8'
    
    ydl_opts = {
        'restrict-filenames': True, 
        'restrictfilenames': True, 
    }
    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        try:
            info = ydl.extract_info("ytsearch:%s" % requestedAudio, download=False)
        except yt_dlp.utils.DownloadError or yt_dlp.utils.ExtractorError:
            # do something else