Play mp4 (with audio) in Tkinter Python

1.7k views Asked by At

i need play video with audio using module tkinter by giving location of files of ( mp4 and audio ) or mp4 and also both the video and audio should sync

i tried this is my

code-1:

from tkinter import *
import pygame
from PIL import Image, ImageTk
import threading
import imageio



main = Tk()

main.title('video in fullscreen')
photo = PhotoImage(file = "image.png")
main.iconphoto(False, photo)

 

main.attributes('-fullscreen', True) #full screen with no window

video = imageio.get_reader('video.mp4')


def display_video(label):
   pygame.init()
   pygame.mixer.music.load("video.mp3") # i already converted video into mp3 in online for audio
   pygame.mixer.music.play()
   
   for image in video.iter_data():
    
      img = Image.fromarray(image)
     
      image_frame = ImageTk.PhotoImage(image = img)
      
      label.config(image=image_frame)
      label.image = image_frame
        

video_label = Label(main)
video_label.pack()

thread = threading.Thread(target=display_video, args=(video_label,))
thread.start()



main.mainloop()

video and audio played but both didn't sync

code-2:

import cv2
import numpy as np


from ffpyplayer.player import MediaPlayer
video_path="path\video.mp4"
def PlayVideo(video_path):
    video=cv2.VideoCapture(video_path)
    player = MediaPlayer(video_path)
    while True:
        grabbed, frame=video.read()
        audio_frame, val = player.get_frame()
        if not grabbed:
            print("End of video")
            break
        if cv2.waitKey(28) & 0xFF == ord("q"):
            break
        cv2.imshow("Video", frame)
        if val != 'eof' and audio_frame is not None:

            img, t = audio_frame
    video.release()
    cv2.destroyAllWindows()
PlayVideo(video_path)

played video and audio sync but not i expected. I want to play in tkinter in fullscreen i don't know how to run this code into tkinter. hope you understand my problem.

thank you!

0

There are 0 answers