Play .mp4 using python and check if/while it is still playing

后端 未结 1 1409
你的背包
你的背包 2021-01-15 15:19

I\'m using Windows 64 bit. I have tried several libraries. Couldnt\' get pygame to work, couldnt\' get pymedia to install on python 2.7.

Eventually got mplayer for

相关标签:
1条回答
  • 2021-01-15 15:57

    It's a little awkward to do this, due to the streaming nature of this implementation, and the lack of documentation.

    However, this is how you do it:

        p = 'C:\\mymusic.mp4'
        v = VideoPlayback_MPlayer.FromPath(p)
        v.playAsync()
        while v.isPlaying:
            time.sleep(0.1)
    

    Where you have a video player class like this:

    class VideoPlayback_MPlayer:
        def __init__(self, path):
            self.path = path
    
        def playAsync(self):
            import mplayer #pip install mplayer.py and also setup choco install mplayer myself via http://downloads.sourceforge.net/project/mplayer-win32/MPlayer%20and%20MEncoder/r37451%2Bg531b0a3/MPlayer-x86_64-r37451%2Bg531b0a3.7z?r=http%3A%2F%2Foss.netfarm.it%2Fmplayer%2F&ts=1442363467&use_mirror=tcpdiag
            self.isPlaying = True
    
            EOFDetectionArgs = "-msglevel global=6"
            self.player = mplayer.Player(args=EOFDetectionArgs.split(), stderr=None, autospawn=True)
            self.player.stdout.connect(self._EOFDetector)
            self.player.loadfile(self.path) 
            self.player.pause() # someone says online this kicks in the audio http://stackoverflow.com/questions/16385225/play-mp4-using-python-and-check-if-while-it-is-still-playing       
    
        def _EOFDetector(self, stream):
            if stream.startswith('EOF code:'):
                self.isPlaying = False
    
        @property
        def done(self):
            return not self.isPlaying
    
    
        def play(self):
            self.playAsync()
            while self.isPlaying:
                time.sleep(0.00001)        
    
        @staticmethod
        def FromPath(path):
            return VideoPlayback_MPlayer(path)
    
    0 讨论(0)
提交回复
热议问题