Python Forum
Push video to left and remove background
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Push video to left and remove background
#1
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]
"Only Boring People Get Bored"
Reply
#2
Make the video player the same size as the video. Position the video player in the top left corner.
Reply
#3
(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
"Only Boring People Get Bored"
Reply
#4
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.
Reply
#5
(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?
"Only Boring People Get Bored"
Reply
#6
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.
Reply
#7
The video was recorded in portrait. I have the video now in the correct aspect ratio, but it still rotates it to landscape, why?
"Only Boring People Get Bored"
Reply
#8
What happens if the video is recorded in lanscape?
Reply
#9
(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
"Only Boring People Get Bored"
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Recommendations for IMAP Push/IDLE library MikeFlorida 0 839 Jul-15-2023, 05:19 PM
Last Post: MikeFlorida
  PyQT5 - align left frohr 7 4,009 May-07-2022, 09:56 PM
Last Post: deanhystad
  How did one column get left-justified? Mark17 6 1,957 Feb-26-2022, 11:55 PM
Last Post: deanhystad
  Explanation of the left side of this statement please rascalsailor 3 2,526 Sep-09-2020, 02:02 PM
Last Post: rascalsailor
  How to left align logging messages Mekala 3 6,886 Jun-28-2020, 04:04 PM
Last Post: bowlofred
  How to left align the columns SriRajesh 6 3,980 Dec-28-2019, 04:04 PM
Last Post: SriRajesh
  Why is left mouse click not working? yeto 3 6,218 Jul-15-2019, 05:23 AM
Last Post: Yoriz
  str.format rounding to the left of the decimal ClassicalSoul 2 2,499 Mar-27-2019, 11:12 AM
Last Post: perfringo
  Move a character left or right in a file. DreamingInsanity 4 4,878 Mar-21-2019, 07:52 PM
Last Post: DreamingInsanity
  Forcing a github push after major changes Larz60+ 10 7,035 Mar-30-2017, 05:58 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020