0

I am working in visual c++, usually I do it on .NET, because I need a method which is available only on this language. What I want to do is obtain the frames per second of a video file. The best I could make was creating a project with this main() method, in which (after Debug) I could see the result is saving fine in the res variable.

void main() 
{
    // initialize the COM library
    CoInitialize(NULL);

    // get a property store for the video file
    IPropertyStore* store = NULL;
    SHGetPropertyStoreFromParsingName(L"C:\\Users\\Public\\Videos\\Sample Videos\\Wildlife.wmv", 
        NULL, GPS_READWRITE, __uuidof(IPropertyStore), (void**)&store);

    // get the frame rate 
    PROPVARIANT variant;
    store->GetValue(PKEY_Video_FrameRate, &variant);
    int res = variant.intVal;
    store->Release();
}

Now, I want to create this method generic, in order to obtain the frameRate of any video. For example, if the method's name is frameRate:

 char* path = "C:\\Users\\Public\\Videos\\Sample Videos\\Wildlife.wmv";
 int fps = frameRate(path);

Thanks

Matias
  • 541
  • 5
  • 22
  • 1
    `void main` is not legal. Use `int main`. And a `char *` pointing to a string literal is also wrong. The fact that what it's pointing to cannot be modified should be reflected in the type. – chris Jul 23 '13 at 18:49
  • 1
    What exactly are you asking? There is no question in your "question". – Niko Jul 23 '13 at 18:51
  • @Niko, From what I gather, how to turn it into a function taking the path as a parameter. – chris Jul 23 '13 at 18:52
  • 1
    I think you mean "generic" in terms of "general." The term "generic" in programming is commonly used to refer to types that are not known at the time the relevant code is compiled. – cdhowie Jul 23 '13 at 18:53
  • Sorry if I did not explained myself fine. As chris says, I want a function taking the path as a parameter. Of course, this wouldn't be main(), it would look something like frameRate(string) or frameRate(char*), still didn't understand that. – Matias Jul 23 '13 at 19:07

2 Answers2

0

If you don't want to recompile your code for every video path, then you can read the path from the program parameters. To do that, modify you main() as follows:

int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        std::cout << "You have to specify the video path!" << std::endl;
        return 1;
    }

    const char* path = arg[1];

    // Rest of the program logic

    return 0;
}

You can pass more than one parameter, if you want to. Note that there is always at least 1 argument (arg[0] is the program name). For further reading on the topic go here.

podkova
  • 1,019
  • 7
  • 16
0

Does this not work?

int getFrameRate(std::wstring path)
{
    // initialize the COM library
    CoInitialize(NULL);

    // get a property store for the video file
    IPropertyStore* store = NULL;
    SHGetPropertyStoreFromParsingName(path.c_str(), 
        NULL, GPS_READWRITE, __uuidof(IPropertyStore), (void**)&store);

    // get the frame rate 
    PROPVARIANT variant;
    store->GetValue(PKEY_Video_FrameRate, &variant);
    int res = variant.intVal;
    store->Release();

    return res;
}

The assumption here is that SHGetPropertyStoreFromParsingName takes a string as its first parameter. In C++ I recommend staying away from char*, std::string is preferable in almost all situations. The only difficulty I see is making sure path is the correct type.

Shaz
  • 1,376
  • 8
  • 18
  • Better make it a `std::wstring`, preferably take it by const reference, and use `c_str()` to get the C string needed to pass to the function. – chris Jul 23 '13 at 19:22
  • I am having this error in the first line (std is underlined): IntelliSense: A name followed by '::' must be a class name or spacename – Matias Jul 23 '13 at 19:28
  • @chris Both excellent points. I had forgotten about c_str() all together. – Shaz Jul 23 '13 at 19:30
  • @Matias make sure to `#include ` at the top of the file. – Shaz Jul 23 '13 at 19:30
  • Thanks Ryan. The problem now is that SHGetPropertyStoreFromParsingName takes a PCWSTR as its first parameter, which I am not pretty sure what is. – Matias Jul 23 '13 at 19:36
  • Found the answer [here](http://stackoverflow.com/questions/27220/how-to-convert-stdstring-to-lpcwstr-in-c-unicode) – Matias Jul 23 '13 at 19:51