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

拟墨画扇 提交于 2019-11-26 06:16:49

问题


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.

Thanks.


回答1:


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. 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.




回答2:


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);
}



回答3:


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.



来源:https://stackoverflow.com/questions/2401764/can-ffmpeg-be-used-as-a-library-instead-of-a-standalone-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!