用C编写的ffmpeg程序,无法在macOS Catalina 10.15.7中打开我的相机

发布于 2025-01-09 04:21:19 字数 2604 浏览 0 评论 0原文

#include <stdio.h>
#include <stdlib.h>
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavdevice/avdevice.h"

AVFormatContext* openCamera(void) {
    avdevice_register_all();
    AVFormatContext *ctx = avformat_alloc_context();
    AVInputFormat *ifmt = av_find_input_format("avfoundation");
    if (ifmt != NULL) {
        AVDictionary *opts = NULL;
        av_dict_set(&opts, "video_size", "1280x720", 0);
        av_dict_set(&opts, "framerate", "30", 0);
        av_dict_set(&opts, "pixel_format", "uyvy422", 0);
        
        int ret = avformat_open_input(&ctx, "0", ifmt, &opts);
        if (ret != 0) {
            printf("no");
            avformat_free_context(ctx);
            return NULL;
        }
    }
    return ctx;
}

int main(int argc, const char *argv[]) {
    openCamera();
}

我的代码在 Xcode 上运行。添加 plist 文件后,正在运行的程序相机打开大约半秒,然后关闭。和控制台输出

2022-02-22 00:39:19.372178+0800 ffmpeg-tool[5977:195724] [plugin] AddInstanceForFactory: No factory registered for id <CFUUID 0x10640cf60> F8BB1C28-BAE8-11D6-9C31-00039315CD46
2022-02-22 00:39:19.434783+0800 ffmpeg-tool[5977:195724]  HALC_ShellDriverPlugIn::Open: Can't get a pointer to the Open routine
2022-02-22 00:39:19.435262+0800 ffmpeg-tool[5977:195724]  HALC_ShellDriverPlugIn::Open: Can't get a pointer to the Open routine
2022-02-22 00:39:19.501780+0800 ffmpeg-tool[5977:195724] [plugin] AddInstanceForFactory: No factory registered for id <CFUUID 0x10640f860> 30010C1C-93BF-11D8-8B5B-000A95AF9C6A
2022-02-22 00:39:19.605797+0800 ffmpeg-tool[5977:195724] Metal API Validation Enabled
2022-02-22 00:39:21.701071+0800 ffmpeg-tool[5977:195724] [] CMIOHardware.cpp:917:CMIODeviceStopStream the System is exiting
2022-02-22 00:39:21.701268+0800 ffmpeg-tool[5977:195724] [] CMIOHardware.cpp:1332:CMIOStreamCopyBufferQueue the System is exiting
2022-02-22 00:39:21.701538+0800 ffmpeg-tool[5977:195724] [] CMIOHardware.cpp:333:CMIOObjectGetPropertyData the System is exiting
2022-02-22 00:39:21.701767+0800 ffmpeg-tool[5977:195724] [] CMIO_DALA_System.cpp:264:GetPropertyData error 1970171760 (unop) getting property selector (inot) scope (glob) element 0
2022-02-22 00:39:21.702472+0800 ffmpeg-tool[5977:195724] [] CMIOHardware.cpp:333:CMIOObjectGetPropertyData the System is exiting
2022-02-22 00:39:21.702662+0800 ffmpeg-tool[5977:195724] [] CMIO_DALA_System.cpp:264:GetPropertyData error 1970171760 (unop) getting property selector (inot) scope (glob) element 0
Program ended with exit code: 0

如何解决这个问题?

#include <stdio.h>
#include <stdlib.h>
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavdevice/avdevice.h"

AVFormatContext* openCamera(void) {
    avdevice_register_all();
    AVFormatContext *ctx = avformat_alloc_context();
    AVInputFormat *ifmt = av_find_input_format("avfoundation");
    if (ifmt != NULL) {
        AVDictionary *opts = NULL;
        av_dict_set(&opts, "video_size", "1280x720", 0);
        av_dict_set(&opts, "framerate", "30", 0);
        av_dict_set(&opts, "pixel_format", "uyvy422", 0);
        
        int ret = avformat_open_input(&ctx, "0", ifmt, &opts);
        if (ret != 0) {
            printf("no");
            avformat_free_context(ctx);
            return NULL;
        }
    }
    return ctx;
}

int main(int argc, const char *argv[]) {
    openCamera();
}

My code run on Xcode. After I have added the plist file, the running program camera opens for about half a second and then closes. and console output

2022-02-22 00:39:19.372178+0800 ffmpeg-tool[5977:195724] [plugin] AddInstanceForFactory: No factory registered for id <CFUUID 0x10640cf60> F8BB1C28-BAE8-11D6-9C31-00039315CD46
2022-02-22 00:39:19.434783+0800 ffmpeg-tool[5977:195724]  HALC_ShellDriverPlugIn::Open: Can't get a pointer to the Open routine
2022-02-22 00:39:19.435262+0800 ffmpeg-tool[5977:195724]  HALC_ShellDriverPlugIn::Open: Can't get a pointer to the Open routine
2022-02-22 00:39:19.501780+0800 ffmpeg-tool[5977:195724] [plugin] AddInstanceForFactory: No factory registered for id <CFUUID 0x10640f860> 30010C1C-93BF-11D8-8B5B-000A95AF9C6A
2022-02-22 00:39:19.605797+0800 ffmpeg-tool[5977:195724] Metal API Validation Enabled
2022-02-22 00:39:21.701071+0800 ffmpeg-tool[5977:195724] [] CMIOHardware.cpp:917:CMIODeviceStopStream the System is exiting
2022-02-22 00:39:21.701268+0800 ffmpeg-tool[5977:195724] [] CMIOHardware.cpp:1332:CMIOStreamCopyBufferQueue the System is exiting
2022-02-22 00:39:21.701538+0800 ffmpeg-tool[5977:195724] [] CMIOHardware.cpp:333:CMIOObjectGetPropertyData the System is exiting
2022-02-22 00:39:21.701767+0800 ffmpeg-tool[5977:195724] [] CMIO_DALA_System.cpp:264:GetPropertyData error 1970171760 (unop) getting property selector (inot) scope (glob) element 0
2022-02-22 00:39:21.702472+0800 ffmpeg-tool[5977:195724] [] CMIOHardware.cpp:333:CMIOObjectGetPropertyData the System is exiting
2022-02-22 00:39:21.702662+0800 ffmpeg-tool[5977:195724] [] CMIO_DALA_System.cpp:264:GetPropertyData error 1970171760 (unop) getting property selector (inot) scope (glob) element 0
Program ended with exit code: 0

How can I solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

活泼老夫 2025-01-16 04:21:19

从您发布的代码来看,它所做的只是打开相机,然后返回到主程序,程序完成后它将退出并关闭连接。如果您想保持连接打开并从相机读取数据,您可能需要开始循环读取数据包。

From the code you posted it just looks like all it does is open the camera and then return to main where it would exit and close the connection when the program is done. You probably want to start reading packets in a loop if you want to keep the connection open and read from the camera.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文