ffmpeg中avio_open无法打开中文路径的解决方法

博客介绍了将文件路径转换为UTF - 8编码的方法,虽未详细说明具体实现,但提及可参考相关链接https://www.cnblogs.com/youfal/archive/2012/09/26/2704094.html 。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将文件路径转换成为UTF-8编码即可,具体实现如下:

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)); 
    memset(UTF8code, 0, nUnicodeSize+1);
    // 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)); 
    UTF8code[nUTF8codeSize+1] = '\0';
    return nUTF8codeSize;
}

...

if (!(fmt->flags & AVFMT_NOFILE))
        {            
            char filename[256];
            ANSIToUTF8((char*)encoderparam->outfilename, filename);
            if (avio_open(&oc->pb, filename, AVIO_FLAG_WRITE) < 0)
            {
                return URL_OPEN_FAILD;
            }
        }

即可。

转载于:https://www.cnblogs.com/youfal/archive/2012/09/26/2704094.html

### FFmpeg 中 `avio_open` 函数的详细信息 #### 函数概述 `avio_open` 是 FFmpeg 提供的一个简化版接口,用于打开输入/输出文件资源。它实际上是基于更复杂的 `avio_open2` 实现的,因此其功能相对有限[^1]。 该函数原型如下: ```c int avio_open(AVIOContext **s, const char *url, int flags); ``` - 参数解释: - `AVIOContext **s`: 输出指针,指向新创建的 `AVIOContext` 对象。 - `const char *url`: 文件路径或者网络协议地址(如 RTMP、UDP 或 HTTPS)。 - `int flags`: 打开模式标志位,可以是 `AVIO_FLAG_READ`, `AVIO_FLAG_WRITE` 或者两者组合。 返回值表示操作成功与否:如果成功则返回0;失败时返回负数错误码[^2]。 #### 工作机制 内部实现上,`avio_open` 调用了更为通用的 `avio_open2` 方法,并省略了一些高级选项参数设置部分。具体流程涉及到了两个重要辅助方法——`ffurl_open()` 和 `ffio_fdopen()` 的协作完成整个过程[^3]: 1. 首先通过调用 `ffurl_open()` 来配置底层 URL 上下文 (URLContext),这一步骤负责处理实际连接建立以及相应属性设定等工作; 2. 接着利用所得结果进一步构建高层抽象层即 AVIOContext 结构实例化对象,则由 `ffio_fdopen()` 完成此任务。 #### 示例代码 下面给出一段简单的 C 语言程序片段展示如何运用上述知识点来读取本地视频文件基本信息: ```c #include <stdio.h> #include <stdlib.h> #include "libavformat/avformat.h" void read_file(const char* filepath){ AVFormatContext *fmt_ctx = NULL; // Allocate an I/O context. if(avio_open(&fmt_ctx->pb, filepath, AVIO_FLAG_READ) != 0){ fprintf(stderr,"Could not open file '%s'\n",filepath); exit(1); } // Open the input video stream and retrieve information about it. if(int ret = avformat_open_input(&fmt_ctx, filepath,NULL,NULL)!=0){ printf("Error opening input file: %s\n",av_err2str(ret)); goto end; } if((ret=avformat_find_stream_info(fmt_ctx, NULL))<0){ fprintf(stderr,"Could not find stream info.\n"); goto end; } // Print summary of the file's contents to stdout. av_dump_format(fmt_ctx, 0, filepath, 0); end: if(fmt_ctx!=NULL && fmt_ctx->pb!=NULL) avio_closep(&fmt_ctx->pb); } int main(){ const char* path="test.mp4"; read_file(path); } ``` 注意,在真实项目开发过程中还需要考虑更多边界条件检测与异常捕获逻辑等内容以提高健壮性和稳定性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值