C语言 FFmpeg 可以用作库而不是独立程序吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2401764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:46:25  来源:igfitidea点击:

Can FFmpeg be used as a library, instead of a standalone program?

cffmpegmpegogglibavcodec

提问by

I'd like to add video conversion capabilities to a program I'm writing. FFmpeg's command line interface for doing this is simply ffmpeg -i InputFile OutputFile, but is there a way to make use of it as a library, so I can do something like ffmpeg_convert(InputFile, OutputFile)?

我想为我正在编写的程序添加视频转换功能。FFmpeg 用于执行此操作的命令行界面很简单ffmpeg -i InputFile OutputFile,但是有没有办法将其用作库,以便我可以执行以下操作ffmpeg_convert(InputFile, OutputFile)

I'm hoping I won't have to use libavcodec directly, as I imagine it will be far more complex than a one-line function to convert between formats. If FFmpeg can't be easily retrofitted to do this, is there perhaps another library based on it that does? I've heard of libvlc, but that seems to only expose a video playingAPI, not video conversion.

我希望我不必直接使用 libavcodec,因为我想它会比在格式之间转换的单行函数复杂得多。如果 FFmpeg 不能轻易改装来做到这一点,那么是否有另一个基于它的库呢?我听说过 libvlc,但这似乎只公开了视频播放API,而不是视频转换。

Thanks.

谢谢。

采纳答案by Christoph

You need libavcodecand libavformat. The FAQtells you:

你需要libavcodeclibavformat。该FAQ告诉你:

4.1 Are there examples illustrating how to use the FFmpeg libraries, particularly libavcodec and libavformat?

Yes. Read the Developers Guide of the FFmpeg documentation. Alternatively, examine the source code for one of the many open source projects that already incorporate FFmpeg at (projects.html).

4.1 是否有示例说明如何使用 FFmpeg 库,尤其是 libavcodec 和 libavformat?

是的。阅读 FFmpeg 文档的开发人员指南。或者,检查已在 ( projects.html) 中包含 FFmpeg 的众多开源项目之一的源代码。

The FFmpeg documentation guide can be found at ffmpeg.org/documentation.html, including the Developer's guide. I suggest looking at libavformat/output-example.cor perhaps the source of the ffmpegcommand line utility itself.

FFmpeg 文档指南可以在ffmpeg.org/documentation.html 中找到,包括开发人员指南。我建议查看libavformat/output-example.c或查看ffmpeg命令行实用程序本身的来源。

回答by Terrence

If you just wanted to make a call to ffmpeg as function rather than a system call, you can do that pretty easily.

如果您只想将 ffmpeg 作为函数而不是系统调用进行调用,那么您可以很容易地做到这一点。

in ffmpeg.c, change:

在 ffmpeg.c 中,更改:

int main(int argc, char **argv) to int ffmpeg((int argc, char **argv)

Then in your call the ffmpeg function and pass in an array that mimics the command line. To make it even easier use a function to create the argc, argv variables.

然后在您调用 ffmpeg 函数并传入一个模仿命令行的数组。为了更轻松地使用函数来创建 argc、argv 变量。

static int setargs(char *args, char **argv)
{
    int count = 0;

    while (isspace(*args)) ++args;
    while (*args) {
        if (argv) argv[count] = args;
        while (*args && !isspace(*args)) ++args;
        if (argv && *args) *args++ = '##代码##';
        while (isspace(*args)) ++args;
        count++;
    }
}

char **parsedargs(char *args, int *argc)
{
    char **argv = NULL;
    int    argn = 0;

    if (args && *args
        && (args = strdup(args))
        && (argn = setargs(args,NULL))
        && (argv = malloc((argn+1) * sizeof(char *)))) {
          *argv++ = args;
          argn = setargs(args,argv);
    }

    if (args && !argv) free(args);

    *argc = argn;
    return argv;
}

void freeparsedargs(char **argv)
{
    if (argv) {
        free(argv[-1]);
        free(argv-1);
    }
}
    return count;
}

int main()
{
    char **argv;
    char *cmd;
    int argc;

    cmd = "ffmpeg -i infile outfile";
    argv = parsedargs(cmd,&argc);
    ffmpeg(argc, argv);
}

回答by deddihp

Yes you have to use libavcodec and libavformat. I think you should read about ffplay.c inside ffmpeg source code. I think it would be easier for you to start with that file.

是的,您必须使用 libavcodec 和 libavformat。我认为您应该在 ffmpeg 源代码中阅读有关 ffplay.c 的内容。我认为您从该文件开始会更容易。