Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a mp4 file with python?

Tags:

python

I was trying to make a script that would play a movie using the default windows application but when I try to run this I get the error: coercing to Unicode: need string or buffer, function found

How should I proceed with this?

import os

print 'Push "enter" to play movie'
raw_input()

def filename():
   filename = movie.mp4
   os.system("start " + filename)

open(filename)
like image 641
Slumpe Avatar asked Jan 23 '14 16:01

Slumpe


People also ask

How do I open a video file in Python?

If you just want to play an mp4 video, then this opencv program will help you to do that. 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.


1 Answers

The problem you're having is that you likely have a variable named movie, and when you do filename = movie.mp4 it's setting assigning the movie's function mp4 to the variable filename. In any case, I don't think there's a reason to do this.

def play_movie(path):
    from os import startfile
    startfile(path)

That's literally all you need for your "Play" function. If I were you, I'd wrap it in a class, something like:

class Video(object):
    def __init__(self,path):
        self.path = path

    def play(self):
        from os import startfile
        startfile(self.path)

class Movie_MP4(Video):
    type = "MP4"

movie = Movie_MP4(r"C:\My Documents\My Videos\Heres_a_file.mp4")
if raw_input("Press enter to play, anything else to exit") == '':
    movie.play()
like image 134
Adam Smith Avatar answered Oct 23 '22 03:10

Adam Smith