Inspect video from ZIP in Python
I have a number of zip files with videos and other files. I'm interested in extracting the duration of each video, in order to calculate the total duration per zip file. I found this article on Kaggle: https://www.kaggle.com/humananalog/examine-mp4-files-with-python-only. It should work, but it gets stuck in some MP4s.
My main obstacle to using openCV so far is that it doesn't allow me to read from a buffer. It's locked to either file name or device number. I found an answer that seems to mention some read_buffer function, but it is available under C++ and I'm doing Python.
This is what I got so far:
from zipfile import ZipFile as ZF
file = 'path/to/file.zip'
z = ZF(file,'r')
count = 0
total = 0
for info in z.infolist():
if info.filename.endswith('.mp4'):
# pp(list(map(lambda a: {a: info.__getattribute__(a)}, info.__dir__())))
count += 1
print(f'{count}. {info.filename}')
vid = z.read(info.filename)
print(len(vid))
total += len(vid)
print(f'Read {total} bytes in {count} files.')
Is there any way to read that vid
buffer into cv2
and get its duration in seconds in any way that doesn't involve saving a file to disk and reading it back through cv2.VideoCapture
? I'm using openCV 4.2.0 under Python 3.7.6 and I could update, if needed.
Many thanks!
link, please ?