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格式即可。

猜你喜欢

转载自blog.csdn.net/qq_41866437/article/details/87947678