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

3

56

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.

Deerstalker answered 8/3, 2010 at 13:59 Comment(8)
Just out of curiosity, why would you prefer to use it as a DLL? What is the disadvantage of exec()ing it?Clipfed
I would not like to run it in a separate process, because AFAIK I would have no ability to monitor its progress or run a function upon its completion.Deerstalker
IMHO running ffmpeg in a seperate process is better idea, so your application won't be stuck while the lengthy process of video encoding goes.Nivernais
Well I can always run it in a different thread; my program is already multithreaded so that isn't an issue.Deerstalker
how to do it version #2641960Seminal
@PShved 10 years later, it's a bad idea to run ffmpeg as separate process on Android, and it has never been a viable option on iOS. I suggest the ffmpeg-kit library that wraps the ffmpeg in a library that can be used similar to the ffmpeg CLI for Android and iOS apps.Khamsin
@AlexCohn ah, what ideas in software are still good 10 years down the road? In this decade, I have learned about sandboxing, cloud, smartphones, vulnerabilities of video codecs, and lots of other cool things. Everything has tradeoffs.Nivernais
I think all ideas , the basics have not changed in 10 years. Knowing that a library is always better the and exe (unless you can write your exe — unless on iOS) is also timeless 50 year old wisdomClerissa
I
34

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.

Interim answered 8/3, 2010 at 16:7 Comment(1)
it is also good to check out FFMPEG_INSTALLED_PATH/share/ffmpeg/examples , lots of sample code is located at there.Multiply
A
27

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);
}
Acquaintance answered 2/7, 2010 at 15:30 Comment(4)
Rather than changing it, just call the compiler with -Dmain=ffmpeg.Magnitogorsk
You also need to deal with the output (normally goes to stdin/stderr) and error handling (is it ok if it calls abort() or exit()?).Ureide
Where download ffmpeg.c ?Access
@Access git repo mirror at github.com/FFmpeg/FFmpeg/blob/master/fftools/ffmpeg.cToxic
K
9

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.

Kanchenjunga answered 17/3, 2010 at 9:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.