简体   繁体   English

如何在C ++中检查给定的文件是否是有效的视频文件?

[英]How to check whether a given file is a valid video file in C++?

I tried to do it using OpenCV: 我尝试使用OpenCV做到这一点:

std::string input_name = "<path to file>";
cv::VideoCapture cap(input_name);
if(!cap.isOpened()) printf("Invalid video\n");

However, when I try passing a .txt file, VideoCapture reads it successfully for unknown reasons. 但是,当我尝试传递.txt文件时,VideoCapture出于未知原因成功读取了该文件。 When I try to find the number of frames in the video, 当我尝试查找视频中的帧数时,

if(cap.get(cv::CV_CAP_PROP_FRAME_COUNT) == 0) printf("Invalid video\n");

I get a different number of frames each time I execute. 每次执行时,我得到的帧数都不相同。 I also tried finding the width and height of a frame in the file, 我还尝试在文件中查找框架的宽度和高度,

cv::Mat Frame;
if(!cap.read(Frame)) printf("Cannot read frame\n");
else
{
    printf("Frame width is %d\n", Frame.cols);
    printf("Frame height is %d\n", Frame.rows);
}

their values are 640 and 400 respectively. 它们的值分别为640和400。 Why is this happening? 为什么会这样呢? Is there a way to check if a given file is a valid video file preferably using OpenCV? 有没有一种方法可以检查给定的文件是否是有效的视频文件,最好使用OpenCV?

The only really good way to determine if a file is "valid", is to read its content with an appropriate reader-function that understands that very format. 确定文件是否“有效”的唯一真正好方法是使用具有适当格式的读取器功能来读取其内容。 There is no simple, universal marker to say "this is a genuine video file, and can't be something else" - and even if there was, someone could on purpose or by mistake put that in the file so that it looks like a "real video", but isn't when you read the rest of the information. 没有简单的通用标记可以说“这是一个真实的视频文件,不能再有其他东西了”-即使存在,有人可能有意或无意地将其放入了文件中,因此看起来像是“真实视频”,但当您阅读其余信息时不是。 Since there are dozens of different video file formats, it's hard to do something more sensible than "try it, see what happens". 由于存在数十种不同的视频文件格式,因此很难做到比“尝试一下,看看会发生什么”更明智的事情。

As discussed in another answer, cv::VideoCapture::read() will read the file and return an error indication if the file is not valid. 如另一个答案中所述, cv::VideoCapture::read()将读取文件,并在文件无效时返回错误指示。 If you want to distinguish between "file doesn't exist", you could do some checking before you try to read it, so you can say "file doesn't exist" as opposed to "sorry, but it seems like that's not a valid video file" - although the latter does apply to "there is no such file" too. 如果要区分“文件不存在”,则可以在尝试读取文件之前进行一些检查,因此可以说“文件不存在”,而不是“抱歉,但这似乎不是”有效视频文件”-尽管后者也适用于“也没有这样的文件”。

Note that I expect cv::VideoCapture to say isOpened() if the file exists, so for a text-file or PDF, it will happily accept that as "opened". 请注意,如果文件存在,我希望cv::VideoCapture会说isOpened() ,因此对于文本文件或PDF,它将很乐意接受为“打开”。 You have to actually try to READ the file to tell if it's a video file. 您实际上必须尝试读取该文件以判断它是否为视频文件。 However, the documentation for both ::get and ::read is pretty useless in respect of "What happens on bad input", it almost seems like the functions are designed to only work with valid input. 但是,就“输入错误会发生什么”而言, ::get::read的文档都几乎没有用,似乎这些函数仅设计用于有效输入。

I'm not familiar with openCV but you can always check for file extension. 我不熟悉openCV,但您始终可以检查文件扩展名。

kdt wrote a method for this in this question https://stackoverflow.com/a/874160/4612406 kdt为此问题写了一个方法https://stackoverflow.com/a/874160/4612406

bool hasEnding (std::string const &fullString, std::string const &ending) 
{
    if (fullString.length() >= ending.length()) {
        return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
    } else {
        return false;
    }
}

Just pass the name of the file and the file extension you want like ".mp4". 只需传递文件名和所需的文件扩展名即可,例如“ .mp4”。 You can change this method to take vector for ending string and check for every string in the vector where you can place .mp4 .avi etc. 您可以更改此方法以采用向量作为结束字符串,并检查向量中可以放置.mp4 .avi等的每个字符串。

you can use the method VideoCapture::read() , that will return false if: 您可以使用VideoCapture::read() ,如果满足以下条件,则将返回false:

  • the video is corrupt 视频损坏
  • the string path is pointing to an invalid file 字符串路径指向无效的文件

example: 例:

cv::VideoCapture cap(input_name);
auto x = cap.read(im);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查cmake给定的头文件是否可用于C ++项目 - How to check in cmake whether a given header file is available for C++ project c ++中是否有任何功能可以检查在特定路径中给定的文件是否为脚本(.sh)文件 - is there any function in c++ to check whether a file given in a specific path is a script(.sh) file or not 如何检查ifstream是否是C ++文件的结尾 - How to check whether ifstream is end of file in C++ 如何使用C ++检查tlb文件是否在注册表中注册? - How to check whether the tlb file is registered in registry using C++? 如何在C ++中检查文件在Qt中是否存在 - How to check whether file exists in Qt in c++ 检查给定的密钥对是否有效 C++ - Check if given keypair is valid C++ 如何检查C ++是否将std :: cout重定向到文件? - How can I check in C++ whether std::cout is redirected to a file? 如何检查C ++中的文件是否存在于Windows程序中? - How do I check whether a file exists in C++ for a Windows program? 如何在C ++中读取输入文件时检查是否出现整数或字符 - How to check whether an integer has occurred or a character has occurred while reading input file in C++ 如何在非托管 C++ 代码中检查字符串是否具有有效的文件路径或目录路径格式? - How to check if a string has a valid file path or directory path format in unmanaged C++ code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM