简体   繁体   中英

How to play mp4 at normal speed on jupyter notebook using python opencv

Are there any ways I can play mp4 videos with the same speed as the actual file on jupyter notebook using python opencv?
When I make .py file and use cv2.imshow() then the speed is normal, but when I try the code below on jupyter notebook, it gets very slow.
My code:

import cv2
import matplotlib.pyplot as plt
from IPython import display
%matplotlib inline

cap = cv2.VideoCapture('video.mp4')

while(cap.isOpened()):
    try:
        ret, frame = cap.read()
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        plt.imshow(frame)

        display.clear_output(wait=True)
        display.display(plt.gcf())

    except KeyboardInterrupt:
        cap.release()

In my experience it's better to use OpenCV's display functions even if you work in Jupyter notebook. This can be achieved like this:

fps = 30
title = 'normal speed video'
delay = int(1000 / fps)

for frame in frames(video):
    cv2.imshow(title, frame)

    if cv2.waitKey(delay) & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()

Here frames generator simply yields video frame by frame and can be found here


Also a video can be displayed in Jupyter programmatically via Video :

from IPython.display import Video

Video(‘https://archive.org/download/Sita_Sings_the_Blues/Sita_Sings_the_Blues_small.mp4’)
Video(‘path/to/video.mp4’)
Video(‘path/to/video.mp4’, embed=True)
Video(b’raw-videodata’, embed=True)

See docs for more info

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM