FFmpeg avformat_open_input 錯誤返回 -5 的可能原因

關於FFmpeg項目中遇到一些問題

  使用FFmpeg編寫錄製音頻程序時,在avformat_open_input 函數處卡住,該函數一直報錯並返回錯誤碼 -5, 百思不得其解,查了很多資料,仍不得解答,後觀看 雷神 文章 發現該問題是由於:
  我的音頻設備名中含有中文字符:

audio=麥克風 (Realtek® Audio)

  需要從ANSI字符格式轉換成UTF-8格式,因爲這是FFmpeg支持的字符格式。
  轉換代碼如下:

	std::string AnsiToUTF8(const char *_ansi, int _ansi_len)
	{
		std::string str_utf8("");
		wchar_t* pUnicode = NULL;
		BYTE * pUtfData = NULL;
		do
		{
			int unicodeNeed = MultiByteToWideChar(CP_ACP, 0, _ansi, _ansi_len, NULL, 0);
			pUnicode = new wchar_t[unicodeNeed + 1];
			memset(pUnicode, 0, (unicodeNeed + 1) * sizeof(wchar_t));
			int unicodeDone = MultiByteToWideChar(CP_ACP, 0, _ansi, _ansi_len, (LPWSTR)pUnicode, unicodeNeed);

			if (unicodeDone != unicodeNeed)
			{
				break;
			}

			int utfNeed = WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)pUnicode, unicodeDone, (char *)pUtfData, 0, NULL, NULL);
			pUtfData = new BYTE[utfNeed + 1];
			memset(pUtfData, 0, utfNeed + 1);
			int utfDone = WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)pUnicode, unicodeDone, (char *)pUtfData, utfNeed, NULL, NULL);

			if (utfNeed != utfDone)
			{
				break;
			}
			str_utf8.assign((char *)pUtfData);
		} while (false);

		if (pUnicode)
		{
			delete[] pUnicode;
		}
		if (pUtfData)
		{
			delete[] pUtfData;
		}

		return str_utf8;
	}

  僅僅需要在使用avformat_open_input函數前,將你的含中文的設備名轉換成UTF-8格式即可。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章