Special characters/kanji problems using Python unicode
Question: I want to use videofileclip(), but a UnicodeDecodeError occurs. The videofiles
include japanese kanji or special characters.

My example code:

    
    
    #-*- coding: utf-8 -*-
    import sys  
    from moviepy.editor import VideoFileClip
    
    reload(sys)  
    sys.setdefaultencoding('utf-8')
    
    a='H:\\kittens.mkv'
    clip1=VideoFileClip(a)
    
    b='H:\\“ēī①”.mp4'
    clip2=VideoFileClip(b)
    
    if clip1.fps >= clip2.fps:
        os.remove(b)
    else:
        os.remove(a)        
    

'a' works fine:

    
    
    >>> a='H:\\kittens.mkv'
    >>> clip=VideoFileClip(a)
    >>> 
    

but 'b' doesn't work:

    
    
    >>> b='H:\\“ēī①”.mp4'
    >>> clip=VideoFileClip(b)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Python27\lib\site-packages\moviepy\video\io\VideoFileClip.py", line 5
    5, in __init__
        reader = FFMPEG_VideoReader(filename, pix_fmt=pix_fmt)
      File "C:\Python27\lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 3
    2, in __init__
        infos = ffmpeg_parse_infos(filename, print_infos, check_duration)
      File "C:\Python27\lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 2
    70, in ffmpeg_parse_infos
        filename, infos))
    UnicodeDecodeError: 'utf8' codec can't decode byte 0xa1 in position 54: invalid
    start byte
    >>> b
    'H:\\\xa1\xb0??\xa8\xe7\xa1\xb1.mp4'
    >>> print b
    H:\“??①”.mp4
    >>> print b.decode('cp949')
    H:\“??①”.mp4
    >>>
    

I've tried this, but it also doesn't work.

    
    
    b=b.decode('cp949')
    b=b.decode('cp949').encode('utf-8')
    b=unicode(b.decode('cp949'))
    

I think that Windows 7 supports Unicode file names (in Japanese kanji or
special characters), but the character set of Python (2.x) (cp949) does not
support special characters. What can I do for this problem?


Answer: Here's a workaround using the
[pywin32](https://sourceforge.net/projects/pywin32) extensions. Basically, you
use the
[`GetShortPathName`](http://timgolden.me.uk/pywin32-docs/win32api__GetShortPathName_meth.html)
function to generate a legacy [8.3
filename](https://en.wikipedia.org/wiki/8.3_filename) from a unicode path.

    
    
    # -*- coding: utf-8 -*-
    
    import os
    import win32api
    from moviepy.editor import VideoFileClip
    
    
    def short_path(unicode_path):
        return win32api.GetShortPathName(unicode_path)
    
    
    v1 = '“ēī①”.mp4'
    print os.path.isfile(v1)  # False
    
    v2 = u'“ēī①”.mp4'
    print os.path.isfile(v2)  # True
    
    # clip = VideoFileClip(v1)  # IOError
    # clip = VideoFileClip(v2)  # UnicodeEncodeError
    clip = VideoFileClip(short_path(v2))  # OK
    print clip.duration
    

