2

I try to run a simple script in Google Colab to determine a length of a video:

!pip install moviepy
from moviepy.editor import *
# Change directory:
os.chdir(r'/content/my_data')
clip = VideoFileClip("my_video.mp4")
print(clip.duration)

However, I obtain an error: imageio.ffmpeg.download() has been deprecated. Use 'pip install imageio-ffmpeg' instead.'

After quick search I found that imageio needs to be downgraded by doing this: !pip install imageio==2.4.1

So I changed the scrip to following:

!pip install moviepy
!pip install imageio==2.4.1
from moviepy.editor import *
# Change directory:
os.chdir(r'/content/my_data')
clip = VideoFileClip("my_video.mp4")
print(clip.duration)

I am still getting the same error.

Any idea how to fix it? Thank you.

user136555
  • 255
  • 2
  • 11

2 Answers2

2

As moviepy is not in great shape nowadays I would recommend just using imageio's V2 API for the time being. It is really easy to get the metadata from a video file using it. Basically,

import imageio
path = "path/to/videofile.mp4"
video = imageio.get_reader(path)

print(video._meta)

Using the meta attribute you can access the number of frames, codec, size, etc. If you only want to get the duration (in seconds) you can just simply call for:

print(video._meta["duration"])
Mr K.
  • 1,064
  • 3
  • 19
  • 22
2

@Mr K.'s answer is correct; however, in modern versions of ImageIO you can use this alternative, which may be simpler:

import imageio.v3 as iio

location = "path/to/videofile.mp4"
duration = iio.immeta(location)["duration"]
print(duration)

(immeta reads the metadata of an image or video.)

FirefoxMetzger
  • 2,880
  • 1
  • 18
  • 32