I want to figure out what Windows does when you right-click a video file and check properties and I would like to write a similar piece of code in C++.
I should be able to figure out how to read the file type and size, but I'm lost in how to get details of the video like resolution and runtime.
Which API commands does Windows use there?

Recommended Answers

All 9 Replies

How would that help me?
That's just another program that does more or less the same Windows does, but doesn't mention which API commands one needs to get this output.
It's open source, meaning one could look into the code and try to figure out the relevant commands but we all know figuring out what someone elses code does takes far longer than developping the same thing from scratch.

It's open source, meaning one could look into the code

That's the way I learn...

There is no real Windows API to read video info, as this info is different for each video format. With the Windows Property System you can read some information, but it is not accessible with all platforms. Personally I'd choose to learn how to read the video formats you need (which IMO works best with an existing open source SDK).

When you asked for runtime you triggered a memory from long ago where MP3 runtimes were at times wildly inaccurate. Just like the man who was turned into a newt in Monty Python who said "I got better", such are better now but I still read complaints it's not exact.

So here's the thing, you may want that runtime but it's not always available and sometimes incorrect. Look at MEDIAINFO at https://mediaarea.net/en/MediaInfo/Support/Formats see what it reports on MKV runtime.

I don't want to give away the answer or spoil it for your research. Do you know why the answer was no for MKV?

Yes, I know, Windows doesn't report details for all video files, sometime it just doesn't and I already guessed that may be because of a file format Windows cannot read correctly.
But then for the video files where Windows does report details within the file properties it gets them correct and it does that in near no time which makes me believe Windows doesn't open the file in it's media player or so to get the properties from there, I do believe Windows must have some API command for that, but of course I'm not sure.

I guess in the end it comes down to ..... that MediaInfo program will also use just API commands, even if wrapped in whatever coding language they use, at the bottom of it, it's just the API.

Regarding "doesn't open the file". I've used so many tools over the years to profile and log what apps access that I can write that Windows does open the file for a peek.

And then we have the problem that many files don't have duration. https://www.loc.gov/preservation/digital/formats/fdd/fdd000105.shtml is for MP3 and it's not in the base spec.

But how do they do it? Spoiler: Everyone cheats. Example: https://stackoverflow.com/questions/20771845/how-to-get-the-size-and-duration-of-an-mp3-file Note this was 2019 and even then, duration isn't in the MP3 file.

This is quite the rabbit hole to go down so I suggest you use MediaInfo or try one of the three functions of https://github.com/stax76/Get-MediaInfo

The Windows Media Foundation API is probably what you are thinking of. You also can just use my example, using the ffmpeg library. Just change the filename.

extern "C" {
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
}

int main() {
    AVFormatContext* fmt_ctx = NULL;
    int ret;

    // read the header of input stream.
    ret = avformat_open_input(&fmt_ctx, "snow.mp4", NULL, NULL);
    if (ret < 0) {
        av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
        return -1;
    }

    // find stream information.
    ret = avformat_find_stream_info(fmt_ctx, NULL);
    if (ret < 0) {
        av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
        return -1;
    }

    // dump info on input or output format.
    av_dump_format(fmt_ctx, 0, "snow.mp4", 0);

    // Close an opened input AVFormatContext.
    avformat_close_input(&fmt_ctx);
    return 0;
}

Output:

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'snow.mp4':
      Metadata:
        major_brand     : isom
        minor_version   : 512
        compatible_brands: isomiso2avc1mp41
        encoder         : Lavf60.3.100
      Duration: 00:00:05.80, start: 0.000000, bitrate: 370 kb/s
        Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 1280x720, 298 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
        Metadata:
          handler_name    : Core Media Video
          encoder         : Lavc60.3.100 libx264
        Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, mono, fltp, 64 kb/s (default)
        Metadata:
          handler_name    : Core Media Audio

Output 2:

Input #0, matroska,webm, from 'ofc.mkv':
  Metadata:
    ENCODER         : Lavf58.29.100
  Duration: 00:00:39.37, start: 0.000000, bitrate: 6474 kb/s
    Stream #0:0: Video: h264 (High), yuv420p(tv, bt709, progressive), 1920x1080 [SAR 1:1 DAR 16:9], 60 fps, 60 tbr, 1k tbn, 120 tbc (default)
    Metadata:
      DURATION        : 00:00:39.367000000
    Stream #0:1: Audio: aac (LC), 48000 Hz, stereo, fltp (default)
    Metadata:
      title           : simple_aac_recording
      DURATION        : 00:00:39.317000000

Thanks @toneewa that looks like what I want, at least a basis from where I can try to work it out for my needs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.