Search code examples
c++ffmpeglibavlibavformat

call avio_open function with non-english filename is invalid


i have been writing unicode based program with libav and i wanna make some file through libav with filename "中.mp4".

this filename is not english, and when i call, function return positive integer(not fail).

but there is "ѱ۰.mp4" instead of "中.mp4". (invalid file name.)

what's the matter?

char * szFilenameA = 0;

#ifdef _UNICODE
    CSHArray<char> aFilenameBuffer;
    aFilenameBuffer.Alloc(lstrlen(szFileName) * 2);
    ZeroMemory(aFilenameBuffer, aFilenameBuffer.GetSize());
    WideCharToMultiByte(CP_ACP, 0, szFileName, lstrlen(szFileName), aFilenameBuffer, aFilenameBuffer.GetSize(), NULL, NULL);
    szFilenameA = aFilenameBuffer;
#else
    szFilenameA = (TCHAR *)szFileName;
#endif

    ZeroMemory(m_pOutputFormatCtx->filename,1024);
    _snprintf(m_pOutputFormatCtx->filename, strlen(szFilenameA), "%s", szFilenameA);

    avio_open(&m_pOutputFormatCtx->pb, szFilenameA, AVIO_FLAG_WRITE)

Solution

  • finally! it's because of charset.

    convert ansi filename to UTF8 and then it works fine.

    int ANSIToUTF8(char *pszCode, char *UTF8code)
    {
      WCHAR Unicode[100]={0,}; 
      char utf8[100]={0,};
    
      // read char Lenth
      int nUnicodeSize = MultiByteToWideChar(CP_ACP, 0, pszCode, strlen(pszCode), Unicode, sizeof(Unicode)); 
    
      // read UTF-8 Lenth
      int nUTF8codeSize = WideCharToMultiByte(CP_UTF8, 0, Unicode, nUnicodeSize, UTF8code, sizeof(Unicode), NULL, NULL); 
    
      // convert to UTF-8 
      MultiByteToWideChar(CP_UTF8, 0, utf8, nUTF8codeSize, Unicode, sizeof(Unicode)); 
      return nUTF8codeSize;
    }