Ask Your Question
0

Inspect video from ZIP in Python

asked 2020-07-17 06:06:21 -0600

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!

edit retag flag offensive close merge delete

Comments

I found an answer that seems to mention some read_buffer function,

link, please ?

berak gravatar imageberak ( 2020-07-17 06:34:45 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-07-17 06:52:22 -0600

berak gravatar image

sad as it is, what you want is not possible.

you'll have to save it to disk, then:

cap = cv2.VideoCapture("my.mp4")
if (cap.isOpened()):
    count = cap.get(cv2.CAP_PROP_FRAME_COUNT)
    fps = cap.get(cv2.CAP_PROP_FPS)
    duration = count/fps

but seriously, if you're on some linux, you should throw it at ffprobe instead

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-07-17 06:04:28 -0600

Seen: 681 times

Last updated: Jul 17 '20