1

I have the following command line that gives me the duration of a video file:

ffprobe.exe -i Video.mp4 show_entries format=duration -v quiet -of csv="p=0"

When I run this, I get back the duration of my video successfully, running it from a command prompt. Now, I'm kind of a beginner with C++, so I'm not sure how I could get that duration to be put into a float inside of my program. I've looked through a lot of articles and question and answer forums online and I've found possible answers with...using stdout/stdin to grab the information...? I'm not sure.

My strategy right now is to use CreateProcess() to run the process, then WaitForSingleObject(), then somehow use some command to grab the output data from the ffprobe process. Perhaps the data I'm looking for can be retrieved through the CreateProcess() function? I feel like I'm very close, but I'm in serious need of help with this...

As a side note, since I'm using Visual C++ Express 2010, I do not have access to MFC.

If anyone could lead me in the right direction to this information it would be greatly appreciated.

  • You could make a batch file that prints the ffmpeg info to a text file, and then calls your program that reads the txt file. It's a fix for your problem but not an answer to your question. – jiggunjer Jul 07 '15 at 12:58
  • That sounds good. I'll try that out as well and tell you how it goes. –  Jul 08 '15 at 19:43

2 Answers2

0

I haven't used ffmpeg myself, but you could the library libavcodec/ffmpeg in your program to get the duration programmatically.

See here for more information: how to use libavcodec/ffmpeg to find duration of video file

Community
  • 1
  • 1
Simon
  • 1,616
  • 2
  • 17
  • 39
  • Thanks. I will try integrating the library into my project and see what I can come up with. –  Jul 08 '15 at 04:05
0

So I used in the end jiggunjer's suggestion of generating a batch file. The software I'm programming is meant to play a movie programatically by loading an external instance of vlc movie player (or any other movie player could be used).

This code is inside of a PlayMovie(char* MovieFileName) function.

This creates the batch file with a single line of code, then executes the batch file, then grabs the data posted from the ini file generated within the Movies// folder.

        //Create batch file
    FILE * pFile;
    //char* movieinfowritebuffer = "ffprobe -v quiet -print_format ini -show_format -show_streams Movies/Kai.mp4 > Movies/Kai.mp4.ini";
    //Minf_wrBuffer = Minf_finalpath
    char* Minf_wrBuffer = &Minf_finalpath[0u];

    pFile = fopen ("MPlayer_MovieInfo.bat", "wb+");
    fwrite (Minf_wrBuffer,strlen(Minf_wrBuffer),1, pFile);
    fclose (pFile);

    Sleep(250);

    //MessageBox(NULL,"Next, we use ShellExecuteA to launch the .bat file.","LAUNCHER",NULL);
    //Run batch file to create mp4 info ini
    ShellExecuteA(NULL,"open","MPlayer_MovieInfo.bat",NULL,NULL,SW_HIDE);

    Sleep(250);

    //MessageBox(NULL,"Now, we grab the duration of the movie file from the ini file using GetPrivateProfileString.","LAUNCHER",NULL);
    //Get duration from ini file

    //Get the filename to create...
    std::string iniFileToCreate = "Movies\\" + Minf_MovieName + ".ini";

    char MovieDurationBuffer[256];
    GetPrivateProfileString("streams.stream.1", 
        "duration", 
        "Default", 
        MovieDurationBuffer, 
        256, 
        iniFileToCreate.c_str());

    float MovieDuration = atof(MovieDurationBuffer);
   //Now play with MovieDuration
    float MovieDurationTruncated = MovieDuration;
    int tmp = MovieDurationTruncated * 100000; // 44.8 truncated to 44
    MovieDurationTruncated = tmp / 100.0; // 4.4

        char MovieDurationChar[20]; sprintf(MovieDurationChar, "%1.0f", MovieDurationTruncated-3);

        int MovieDurationInt;
        sscanf(MovieDurationChar, "%d", &MovieDurationInt);