【问题标题】:How to capture device input with ffmpeg in C?如何在 C 中使用 ffmpeg 捕获设备输入?
【发布时间】:2019-05-24 10:38:20
【问题描述】:

我正在尝试在我的 C 代码中使用 ffmpeg 来捕获设备输入,例如屏幕和音频记录。我查看了他们的官方文档和 wiki,但与命令行用法相比,API 文档并没有得到很好的解释。

根据文档,如果我想在 linux 上使用 alsa 录制音频,例如

ffmpeg -f alsa -i hw:<#card>,<#device> -t <seconds> out.wav

我想使用 C API 做同样的事情,知道吗?

【问题讨论】:

    标签: c ffmpeg


    【解决方案1】:

    我不确定你的程序到底是干什么用的,但如果没有特别的限制,你可以尝试在 C 中调用 shell 命令:

    /**
     * @brief exec_shcmd - execute a shell command via popen
     *(This function doesn't support write command now. such as 'echo "abc" > abc.txt')
     *
     * @para cmd_line - shell command string
     * @para read_buf - output string buffer after execute command
     * @para len - length of read buffer
     *
     * @return result of shell command execute
     * @retval 0 - success
     * @retcal -1 - failed
     */
    int exec_shcmd(char *cmd_line, char *read_buf, ssize_t len)
    {
        FILE *stream;
    
        if ((cmd_line == NULL) || (read_buf == NULL) || (len == 0)) {
            assert(0);
            return -1;
        }
    
        stream = popen(cmd_line, "r");
        if (stream == NULL) {
            assert(0);
            return -1;
        }
    
        memset(read_buf, 0, len);
        fread(read_buf, sizeof(char), len, stream);
        pclose(stream);
    
        printf("execute a shell command: %s", cmd_line);
        printf("shell command return: %s", read_buf);
    
        return 0;
    }
    
    

    这是我正在使用的功能,您可以根据需要进行更改。你可以这样使用它:

    void main(void)
    {
        char *cmd = "ls -al\n";
        char buf[500];
        int ret;
    
        printf("<-----------cmd exec------------->\n");
    
        ret = exec_shcmd(cmd, buf, sizeof(buf));
        printf("result(0 - success -1 - fail): <%d>\n", ret);
    }
    

    【讨论】:

    • 是的,我知道这是一个选择,但是当ffmpeg为C提供API时,我看不到调用shell的原因。唯一的问题是我不知道如何使用那些API功能。
    • 这里的示例程序有什么需要的吗:ffmpeg.org/doxygen/4.1/examples.html
    • 我之前看过这个,但是我没有找到任何关于如何捕获 pcm 采样的信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-18
    • 2015-11-22
    • 1970-01-01
    • 2013-12-15
    • 2015-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多