Python Forum

Full Version: Push video to left and remove background
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hi All,

I have the below code:

from tkinter import *
from tkinter.filedialog import askopenfile
from tkVideoPlayer import TkinterVideo
from random import *
import cv2
from tkinter import messagebox

global closed
closed = 0

window = Tk()
window.title("Tkinter Play Videos in Video Player")
window.attributes('-fullscreen',True)
window.configure(bg="orange red")

#TODO: get video pushed to left without white

canvas = Canvas(window, bg="orange red")
canvas.pack(expand=True, fill=BOTH)

global next_coord
next_coord = False

def get_coords(event):
    global next_coord
    global coords_x_1
    global coords_y_1
    global coords_x_2
    global coords_y_2
    
    if next_coord == False:
        coords_x_1 = event.x
        coords_y_1 = event.y
        print ((str(coords_x_1) + " " + str(coords_y_1)))
        print ("above is 1")
        next_coord = True

    elif next_coord == True:
        coords_x_2 = event.x
        coords_y_2 = event.y
        print ((str(coords_x_2) + " " + str(coords_y_2)))
        print ("above is 2")


def open_file():
    
    file = askopenfile(mode='r', filetypes=[('Video Files', ["*.mp4"])])
    
    if file is not None:
        global filename
        filename = file.name
        global videoplayer
        videoplayer = TkinterVideo(canvas, scaled=False)
        videoplayer.bind("<Button-1>", get_coords)
        videoplayer.load(r"{}".format(filename))
        videoplayer.pack(side=LEFT, expand=True, fill="both")
        videoplayer.play()
 

def playAgain():
    print(filename)
    videoplayer.play()
 
def StopVideo():
    print(filename)
    videoplayer.stop()
 
def PauseVideo():
    print(filename)
    videoplayer.pause()



#lbl1 = Label(window, text="Tkinter Video Player", bg="orange red", fg="white", font="none 24 bold")
#lbl1.config(anchor=CENTER)
#lbl1.pack()
 
openbtn = Button(window, text='Open', command=lambda: open_file())
openbtn.pack(side=BOTTOM, pady=2)
 
playbtn = Button(window, text='Play Video', command=lambda: playAgain())
playbtn.pack(side=BOTTOM, pady=3)
 
stopbtn = Button(window, text='Stop Video', command=lambda: StopVideo())
stopbtn.pack(side=BOTTOM, padx=4)
 
pausebtn = Button(window, text='Pause Video', command=lambda: PauseVideo())
pausebtn.pack(side=BOTTOM, padx=5)


def on_closing():
    global closed
    if messagebox.askokcancel("Quit", "Do you want to quit?"):
        closed = 1
        window.destroy()

window.protocol("WM_DELETE_WINDOW", on_closing)
window.mainloop()
It allows me to upload a video, then it records the coords of where I click on the video. However, when I upload the video it puts it in the middle, and adds a white background around it. What can I do so it moves into the top left corner, and the white background is removed?

[Image: z7HZLXR]
Make the video player the same size as the video. Position the video player in the top left corner.
(Dec-04-2022, 09:20 PM)deanhystad Wrote: [ -> ]Make the video player the same size as the video. Position the video player in the top left corner.

How would I do this? the video may be a differnt size everytime
You ask the video what size it is? Well, you actually let the video player ask the video what size it is, then you get the video information from the player. I think you've been working with this video player for about 4 months. You should be familiar with what it can do. If not you need to learn. If you don't know your tools you do shoddy work.
(Dec-06-2022, 05:25 PM)deanhystad Wrote: [ -> ]You ask the video what size it is? Well, you actually let the video player ask the video what size it is, then you get the video information from the player. I think you've been working with this video player for about 4 months. You should be familiar with what it can do. If not you need to learn. If you don't know your tools you do shoddy work.

global canvas
def open_file():
     
    file = askopenfile(mode='r', filetypes=[('Video Files', ["*.mp4"])])
     
    if file is not None:
        global canvas
        global filename
        global videoplayer
        
        filename = file.name

        canvas = Canvas(window, bg="orange red")
        canvas.pack(expand=True, fill=BOTH)
        videoplayer = TkinterVideo(canvas, scaled=True)

        video_width = videoplayer.winfo_width()
        video_height = videoplayer.winfo_height()
        print(f"Video width: {video_width}")
        print(f"Video height: {video_height}")
            
        canvas.config(width=video_width, height=video_height)
        canvas.pack(expand=True, fill=BOTH)
        
        videoplayer.bind("<Button-1>", get_coords)
        videoplayer.load(r"{}".format(filename))
        videoplayer.pack(side=LEFT, expand=True, fill="both")
        videoplayer.play()
I've done this instead and this has made the video fit, if it is recorded to certain dimensions. If I record the video on my phone and uplaod it here it then rotates it and stretches it to fit, why?
Are you sure it is rotated and not just the way you held the phone? Stretching to fit makes sense since you set the video player to scale the video. You might want to set keep_aspect_ratio = True if you don't want the image distorted.
The video was recorded in portrait. I have the video now in the correct aspect ratio, but it still rotates it to landscape, why?
What happens if the video is recorded in lanscape?
(Dec-07-2022, 12:32 PM)deanhystad Wrote: [ -> ]What happens if the video is recorded in lanscape?

In landscape it works fine, if i have Scaled = True it fills the whole space, If i have Scaled = False, it wrks, with white either side
The player must not pay attention to the rotation metadata in the video file. That doesn't surprise me. If you want to support portrait, you'll need a button or something the user can click to change between landscape and portrait.

scaled=True is fine. You want to scale the video to fit the view. But you should set keep_aspect_ratio=True so it doesn't change the shape.
Pages: 1 2