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

时间:2022-09-07 07:34:00

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

我希望我不必直接使用libavcodec,因为我认为它将比单行函数更复杂,以便在格式之间进行转换。如果FFmpeg不能轻易改装来执行此操作,是否可能有另一个基于它的库?我听说过libvlc,但这似乎只暴露了一个视频播放API,而不是视频转换。

Thanks.

谢谢。

3 个解决方案

#1


19  

You need libavcodec and libavformat. The FAQ tells you:

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

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

4.1是否有说明如何使用FFmpeg库的示例,特别是libavcodec和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).

是。阅读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.c or perhaps the source of the ffmpeg command line utility itself.

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

#2


23  

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++ = '\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);
}

#3


5  

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. Anyway I have made some application using those library, check it out at rtstegvideo.sourceforge.net.

是的,你必须使用libavcodec和libavformat。我想你应该在ffmpeg源代码中读到关于ffplay.c的内容。我认为从这个文件开始会更容易。无论如何,我已经使用这些库做了一些应用程序,请在rtstegvideo.sourceforge.net上查看。

Hope this help...

希望这有帮助......

#1


19  

You need libavcodec and libavformat. The FAQ tells you:

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

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

4.1是否有说明如何使用FFmpeg库的示例,特别是libavcodec和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).

是。阅读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.c or perhaps the source of the ffmpeg command line utility itself.

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

#2


23  

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++ = '\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);
}

#3


5  

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. Anyway I have made some application using those library, check it out at rtstegvideo.sourceforge.net.

是的,你必须使用libavcodec和libavformat。我想你应该在ffmpeg源代码中读到关于ffplay.c的内容。我认为从这个文件开始会更容易。无论如何,我已经使用这些库做了一些应用程序,请在rtstegvideo.sourceforge.net上查看。

Hope this help...

希望这有帮助......