- OpenCV Python - Digit Recognition
- OpenCV Python - Feature Matching
- OpenCV Python - Feature Detection
- OpenCV Python - Meanshift/Camshift
- OpenCV Python - Face Detection
- OpenCV Python - Video from Images
- OpenCV Python - Images From Video
- OpenCV Python - Play Videos
- OpenCV Python - Capture Videos
- OpenCV Python - Fourier Transform
- OpenCV Python - Image Blending
- OpenCV Python - Image Addition
- OpenCV Python - Image Pyramids
- OpenCV Python - Template Matching
- OpenCV Python - Image Contours
- OpenCV Python - Transformations
- OpenCV Python - Color Spaces
- OpenCV Python - Histogram
- OpenCV Python - Edge Detection
- OpenCV Python - Image Filtering
- OpenCV Python - Image Threshold
- OpenCV Python - Resize and Rotate
- OpenCV Python - Add Trackbar
- OpenCV Python - Mouse Events
- OpenCV Python - Shapes and Text
- OpenCV Python - Bitwise Operations
- OpenCV Python - Image Properties
- OpenCV Python - Using Matplotlib
- OpenCV Python - Write Image
- OpenCV Python - Reading Image
- OpenCV Python - Environment
- OpenCV Python - Overview
- OpenCV Python - Home
OpenCV Python Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
OpenCV Python - Play Video from File
The VideoCapture() function can also retrieve frames from a video file instead of a camera. Hence, we have only replaced the camera index with the video file’s name to be played on the OpenCV window.
video=cv2.VideoCapture(file)
While this should be enough to start rendering a video file, if it is accompanied by sound. The sound will not play along. For this purpose, you will need to install the ffpyplayer module.
FFPyPlayer
FFPyPlayer is a python binding for the FFmpeg pbrary for playing and writing media files. To install, use pip installer utipty by using the following command.
pip3 install ffpyplayer
The get_frame() method of the MediaPlayer object in this module returns the audio frame which will play along with each frame read from the video file.
Following is the complete code for playing a video file along with its audio −
import cv2 from ffpyplayer.player import MediaPlayer file="video.mp4" video=cv2.VideoCapture(file) player = MediaPlayer(file) while True: ret, frame=video.read() audio_frame, val = player.get_frame() if not ret: print("End of video") break if cv2.waitKey(1) == ord("q"): break cv2.imshow("Video", frame) if val != eof and audio_frame is not None: #audio img, t = audio_frame video.release() cv2.destroyAllWindows()Advertisements