54

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.

  • Just out of curiosity, why would you prefer to use it as a DLL? What is the disadvantage of exec()ing it? – Jonathan Feinberg Mar 08 '10 at 14:14
  • 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. –  Mar 08 '10 at 14:19
  • 2
    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. – P Shved Mar 08 '10 at 14:26
  • Well I can always run it in a different thread; my program is already multithreaded so that isn't an issue. –  Mar 08 '10 at 14:27
  • how to do it version http://stackoverflow.com/questions/2641460/ffmpeg-c-api-documentation-tutorial – Ciro Santilli OurBigBook.com Feb 20 '16 at 22:07
  • @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](https://github.com/tanersener/ffmpeg-kit) library that wraps the **ffmpeg** in a library that can be used similar to the ffmpeg CLI for Android and iOS apps. – Alex Cohn Apr 04 '21 at 19:39
  • 2
    @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. – P Shved Apr 06 '21 at 02:30

3 Answers3

32

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.

Community
  • 1
  • 1
Christoph
  • 164,997
  • 36
  • 182
  • 240
  • it is also good to check out `FFMPEG_INSTALLED_PATH/share/ffmpeg/examples` , lots of sample code is located at there. – Ham Jun 08 '22 at 16:44
26

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);
}
Terrence
  • 269
  • 3
  • 2
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.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
deddihp
  • 653
  • 1
  • 6
  • 17