So I know of opencv and pymovie however I cant seem to find a working example on here or anywhere on google or this site. I do not want to convert the mp4 into anything else. It has to be mp4 file format video if at all possible. If possible can someone please point me in the direction of a module I can use to achieve this and an example of a script I can use from said module or modules to get an mp4 to play. If not possible what is a good format I should convert to and what is a good program for convertion. All input would be helpful thanks.
How do I open an MP4 video file with python?
what do you want to do with the mp4 file after you open it in python? –
Caernarvonshire
I just want to be able to play it in my own window –
Leff
are you using something like PyQt for the user interface or something else to show the video in? –
Caernarvonshire
i was thinking like in pygame or tkinter or something of the like –
Leff
Looks like some people had success using tkinter –
Caernarvonshire
can you give me an example on how that would work? –
Leff
@Eli would a wrapper for the ffmpeg work? –
Leff
If you just want to play an mp4 video, then this opencv program will help you to do that.
import cv2
cap = cv2.VideoCapture("v2.mp4")
ret, frame = cap.read()
while(1):
ret, frame = cap.read()
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q') or ret==False :
cap.release()
cv2.destroyAllWindows()
break
cv2.imshow('frame',frame)
You can change the name of the window as you like. If video file is in another directory, then give the patch of the video in the format 'Drive://x/x/xx.mp4'. 'ret' value shows whether the video file is read or not, frame wise.
pip3 install opencv-python
for anyone who hasn't gotten as far as the OP but wants to :) It's bigger than most libraries - 92.1MB
–
Eatage What's wrong with just running the file?
from os import startfile
startfile(path)
If you have any .mp4 player on your computer it will play.
this does not answer the question because i wanted to display video in opencv like window.. i could have gotten this far with the code you provided . –
Leff
Interesting to know for Python newbies even if it's not for the OP's goal. pypi.org/project/universal-startfile.
It emulates the following actions in an operating system's UI: double-clicking a file or single-clicking a URL.
For people who don't have Mac OS's open
command this could be useful. –
Eatage I encountered audio problems with the first solution, and I don't like that the second solution doesn't close the window. Therefore, I suggest using Pygame and Pyvidplayer2.
Setup:
pip install pygame
pip install pyvidplayer2
Code:
import pygame
from pyvidplayer2 import Video
# create video object
vid = Video("video.mp4")
win = pygame.display.set_mode(vid.current_size)
pygame.display.set_caption(vid.name)
while vid.active:
key = None
for event in pygame.event.get():
if event.type == pygame.QUIT:
vid.stop()
elif event.type == pygame.KEYDOWN:
key = pygame.key.name(event.key)
# only draw new frames, and only update the screen if something is drawn
if vid.draw(win, (0, 0), force_draw=False):
pygame.display.update()
pygame.time.wait(16) # around 60 fps
# close video when done
vid.close()
pygame.quit()
© 2022 - 2024 — McMap. All rights reserved.