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

发布于 2024-08-24 16:16:03 字数 288 浏览 6 评论 0原文

我想向我正在编写的程序添加视频转换功能。 FFmpeg 执行此操作的命令行界面只是 ffmpeg -i InputFile OutputFile,但是有没有办法将其用作库,所以我可以执行类似 ffmpeg_convert(InputFile, OutputFile )?

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

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)?

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 playing API, not video conversion.

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

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

发布评论

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

评论(3

溇涏 2024-08-31 16:16:03

您需要 libavcodec 和 libavformat。 常见问题解答告诉您:

4.1 是否有示例说明如何使用 FFmpeg 库,特别是 libavcodeclibavformat

是的。检查源存储库中的 doc/examples 目录,也可在线访问: https: //github.com/FFmpeg/FFmpeg/tree/master/doc/examples

默认情况下也会安装示例,通常位于 $PREFIX/share/ffmpeg/examples 中。

您还可以阅读 FFmpeg 文档的开发人员指南。或者,检查已合并 FFmpeg 的众多开源项目之一的源代码 (projects.html< /a>)。


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

You need libavcodec and libavformat. The FAQ tells you:

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

Yes. Check the doc/examples directory in the source repository, also available online at: https://github.com/FFmpeg/FFmpeg/tree/master/doc/examples.

Examples are also installed by default, usually in $PREFIX/share/ffmpeg/examples.

Also you may 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).

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

孤者何惧 2024-08-31 16:16:03

如果您只想将 ffmpeg 作为函数而不是系统调用进行调用,则可以非常轻松地做到这一点。

在 ffmpeg.c 中,更改:

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

然后在调用 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++ = '\0';
        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);
}

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

In ffmpeg.c, change:

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.

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++ = '\0';
        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);
}
满栀 2024-08-31 16:16:03

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

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.

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