0

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.

  • yes because,the default encoding used by Python on Windows is `cp1252`, which doesn't support all characters! – Freeman Jun 26 '23 at 17:33

1 Answers1

1

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
Freeman
  • 9,464
  • 7
  • 35
  • 58