在Tkinter框架上播放带音频的视频

时间:2019-03-16 14:16:52

标签: python opencv vlc

因此,我正在尝试做一些真正令人讨厌的事情。我正在使用opencv和ffpyplayer尝试在Tk窗口上制作嵌入式视频播放器。

它确实可以工作,但是我在同步音频和视频时遇到问题,我可以得到不错的结果,但是过一会儿它又不再同步。

整个代码:

import time, traceback, os, telepot
from tkinter import *
import cv2, youtube_dl # pip install opencv-python; pip install --upgrade 
youtube_dl
from PIL import Image, ImageTk #
from io import BytesIO # io
from ffpyplayer.player import MediaPlayer # pip install ffpyplayer
from pytube import YouTube

_name_ = os.path.basename(os.path.realpath(__file__))
_path_ = os.path.realpath(__file__).replace(_name_, '')

class Screen(Frame):
    '''
        Screen widget: Embedded video player from local or youtube
    '''
    def __init__(self, parent, *args, **kwargs):
        Frame.__init__(self, parent, bg = 'black')
        self.settings = { # Inizialazing dictionary settings
            "width" : 1024,
            "height" : 576
        }
        self.settings.update(kwargs) # Changing the default settings
        # Open the video source |temporary
        self.video_source =  _path_+'asd.mp4'

        # Inizializing video and audio variables
        self.vid = None
        self.aux = None
        # Canvas of the player
        self.canvas = Canvas(self, width = self.settings['width'], height = self.settings['height'], bg = "black", highlightthickness = 0)
        self.canvas.pack()

        # NEED TO SYNC AUDIO
        self.delay = 15 # Delay between frames of player

    def update(self):
        '''
            Function: Start the player and keeps drawing the canvas 
        '''
        if not self.vid or not self.aux: # If Audio or Video is missing stop everything
            self.stop()
            return None

        # Get the frames and if video and audio are running
        ret, frame = self.get_frame()
        audio_frame, val = self.aux.get_frame()

        # Drawing frames on canvas
        if self.fb == 1: # Check if it's the first cycle, trying to make the audio start with the video
            self.photo = ImageTk.PhotoImage(image = Image.fromarray(frame).resize((self.settings['width'], self.settings['height'])))
            self.canvas.create_image(0,0, image = self.photo, anchor = 'nw')
            self.fb = 0
            self.aux.set_pause(False) # Starting the audio
        elif ret and val != 'eof':
            self.photo = ImageTk.PhotoImage(image = Image.fromarray(frame).resize((self.settings['width'], self.settings['height'])))
            self.canvas.create_image(0,0, image = self.photo, anchor = 'nw')

        self.after(self.delay, self.update) # Update for single frame, need to sync

    def get_frame(self):
        '''
            Function: Draws the frames
        '''
        if self.vid.isOpened():
            ret, frame = self.vid.read()
            if ret:
                # Return a boolean success flag and the current frame converted to BGR
                return (ret, cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
            else:
                return (ret, None)

    def youTube(self, ID):
        '''
            Function: Gets the youtube video and starts it 
        '''
        print("(TO REPLACE) : Downloading")
        yt = YouTube("https://www.youtube.com/watch?v=" + ID)
        stream = yt.streams.filter(progressive=True).first() # SEE THE POSSIBLE THINGS TO DOWNLOAD
        stream.download(_path_, 'test')
        print("(TO REPLACE) : Finished")
        self.start(_path_+'\\test.mp4')

    def start(self, _source):
        '''
            Function: Starts the player when gets input from keyboard(temporal) or Telegram
        '''
        try: # Stopping player if is already playing for a new video
            self.stop()
        except:
            None

        ff_opts = {'paused' : True} # Audio options
        self.fb = 1 # Setting first cycle

        if _source == 'local': # Checking which source use
            self.vid = cv2.VideoCapture(self.video_source)
            self.aux = MediaPlayer(self.video_source, ff_opts=ff_opts)
        else:
            self.vid = cv2.VideoCapture(_source)
            self.aux = MediaPlayer(_source, ff_opts=ff_opts)

        if not self.vid.isOpened():
            raise ValueError("Unable to open video source")

        self.update() # Starting the player

    def stop(self):
        '''
            Function: Release and stop Video and Audio
        '''
        try: # Stopping video
            self.vid.release()
            self.vid = None
        except:
            pass
        try: # Stopping audio
            self.aux.toggle_pause()
            self.aux = None
        except:
            pass
        self.canvas.delete('all') # Resetting canvas

    def __del__(self):
        '''
            Function: Release the video source when the object is destroyed
        '''
        if self.vid.isOpened():
            self.vid.release()

class Mirror:
    '''
        Mainframe: Display where to put the widgets
    '''
    def __init__(self):
        self.tk = Tk() # Creating the window
        self.tk.configure(background = 'black')
        self.tk.update()

        # Setting up the FRAMES for widgets
        self.bottomFrame = Frame(self.tk, background = 'black')
        self.bottomFrame.pack(side = BOTTOM, fill = BOTH, expand = YES)

        # Bindings and fullscreen setting
        self.fullscreen = False
        self.tk.bind("<Return>", self.toggle_fullscreen)
        self.tk.bind("<Escape>", self.end_fullscreen)

        # Screen, BOT
        print("Inizializing Screen...")
        self.screen = Screen(self.bottomFrame)
        self.screen.pack(side = TOP)

        self.tk.bind("<Key>", self.key) # Get inputs from keyboard

    def key(self, event):
        pressed = repr(event.char).replace("'", '')
        if pressed == 's':
            self.screen.stop()
        elif pressed == 'a':
            self.screen.start('local')
        else:
            print('fail')

    def toggle_fullscreen(self, event = None):
        self.fullscreen = True
        self.tk.attributes("-fullscreen", self.fullscreen)

    def end_fullscreen(self, event = None):
        self.fullscreen = False
        self.tk.attributes("-fullscreen", self.fullscreen)

    def on_chat_message(msg):
        content_type, chat_type, chat_id = telepot.glance(msg)
        message = str(msg.get('text'))

        if 'https://youtu.be/' in message:
            URL_VIDEO = message.split('https://youtu.be/')[1]
            Mir.screen.youTube(URL_VIDEO)       
        elif 'stop' == message.lower():
            Mir.screen.stop()

if __name__ == '__main__':
    Mir = Mirror()
    #bot = telepot.Bot(TELEGRAM_TOKEN)
    #bot.message_loop(on_chat_message)
    Mir.tk.mainloop()
    #while 1:
        #time.sleep(10)

具体来说,呈现帧的方法:

# NEED TO SYNC AUDIO
    self.delay = 15 # Delay between frames of player

def update(self):
    '''
        Function: Start the player and keeps drawing the canvas 
    '''
    if not self.vid or not self.aux: # If Audio or Video is missing stop everything
        self.stop()
        return None

    # Get the frames and if video and audio are running
    ret, frame = self.get_frame()
    audio_frame, val = self.aux.get_frame()

    # Drawing frames on canvas
    if self.fb == 1: # Check if it's the first cycle, trying to make the audio start with the video
        self.photo = ImageTk.PhotoImage(image = Image.fromarray(frame).resize((self.settings['width'], self.settings['height'])))
        self.canvas.create_image(0,0, image = self.photo, anchor = 'nw')
        self.fb = 0
        self.aux.set_pause(False) # Starting the audio
    elif ret and val != 'eof':
        self.photo = ImageTk.PhotoImage(image = Image.fromarray(frame).resize((self.settings['width'], self.settings['height'])))
        self.canvas.create_image(0,0, image = self.photo, anchor = 'nw')

    self.after(self.delay, self.update) # Update for single frame, need to sync

我尝试了其他方法使此功能正常运行,并寻找是否可以找到解决方案但没有成功,如果有人对此有想法或更好的解决方案,我将不胜感激。

我也尝试以流的形式从youtube获取视频,而无需先下载整个视频才能播放它,我可以使视频正常工作,但是如果有人的话,我找不到办法播放音频我也想知道一种方法。

修改: 因此,视频的帧频应为24、30或60,我应该检查视频的帧频,然后相应地设置延迟,我这样做的方式是,我根据视频手动更改了延迟尝试通过反复尝试使其同步的帧速率。老实说,我对音频不太了解,所以对这些东西一无所知。

要使整个代码运行,还需要电报机器人的令牌或本地视频文件才能播放。

1 个答案:

答案 0 :(得分:0)

好吧,我找到了一个更好的解决方案。我尝试再次使用vlc模块,但在使用它之前遇到了麻烦,但是在了解了它之后,我实际上找到了一种方法,可以将vlc的输出打印到带有画布的框架

这实际上非常简单,我摆脱了cv2和ffpyplayer并使用了一个简单的VLC播放器

请注意,在我的情况下,Screen是一个Frame,因此只需使用'Frame' .winfo_id()即可获取tk帧的ID,以便在其中输出视频,音频也可以。然后只需使用播放器 .set_hwnd(ID)将其设置为vlc播放器即可。

class Screen(Frame):
'''
    Screen widget: Embedded video player from local or youtube
'''
def __init__(self, parent, *args, **kwargs):
    Frame.__init__(self, parent, bg = 'black')
    self.settings = { # Inizialazing dictionary settings
        "width" : 1024,
        "height" : 576
    }
    self.settings.update(kwargs) # Changing the default settings
    # Open the video source |temporary
    self.video_source =  _path_+'asd.mp4'

    # Canvas where to draw video output
    self.canvas = Canvas(self, width = self.settings['width'], height = self.settings['height'], bg = "black", highlightthickness = 0)
    self.canvas.pack()

    # Creating VLC player
    self.instance = vlc.Instance()
    self.player = self.instance.media_player_new()


def GetHandle(self):
    # Getting frame ID
    return self.winfo_id()

def play(self, _source):
    # Function to start player from given source
    Media = self.instance.media_new(_source)
    Media.get_mrl()
    self.player.set_media(Media)

    #self.player.play()
    self.player.set_hwnd(self.GetHandle())
    self.player.play()