user672033
user672033

Reputation:

How to programatically determine video dimensions in windows

I'm trying to add a feature to a program I'm writing to determine the width and height of a video file (which can be selected with a browse.. button). I'm using Python and Qt, and I have looked all over and can't seem to find any help on this. The video format is currently .flv but will be expanded in the future to include other formats such as H.264.

Windows explorer can tell me the video dimensions if I right click on the file and choose properties, then choose the summary tab and click advanced. The width and height displayed there is exactly what I need.

Any ideas?

Thanks

Marlon

Upvotes: 0

Views: 429

Answers (1)

karlphillip
karlphillip

Reputation: 93410

Windows Explorer reads the file header for this info.

I see 3 options:

  • You can parse the header manually looking for metadata info (oh god, please don't)
  • Use a cmd-line tool and parse it's output (check popen() to know how to do that)
  • Use a 3rd party library to retrieve the relevant information from the video file

I'm assuming you want to do what's easier. I think that writing a wrapper around a cmd-line tool like mediainfo for displaying information on video files and then parsing it's output to capture the video properties would be easier. If you already have ffmpeg installed on your system you could go with that too.

Another way of doing it is using 3rd party libraries such as libavformat (which is a part of ffmpeg) to read the video properties. For a complete demo take a look at tutorial01.c:

  // Register all formats and codecs
  av_register_all();

  // Open video file
  if(av_open_input_file(&pFormatCtx, argv[1], NULL, 0, NULL)!=0)
    return -1; // Couldn't open file

  // Retrieve stream information
  if(av_find_stream_info(pFormatCtx)<0)
    return -1; // Couldn't find stream information

  // Find the first video stream
  videoStream=-1;
  for(i=0; i<pFormatCtx->nb_streams; i++)
    if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO) {
      videoStream=i;
      break;
    }
  if(videoStream==-1)
    return -1; // Didn't find a video stream

  // Get a pointer to the codec context for the video stream
  pCodecCtx=pFormatCtx->streams[videoStream]->codec;

  // The relevant structure here is: pCodecCtx
  // More precisely: pCodecCtx->width and pCodecCtx->height

Upvotes: 2

Related Questions