Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add Loop property #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ Below are the methods of this library.

| Methods | Parameters | Description |
|------------------|--------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| \_\_init\_\_ | scaled(bool), consistant_frame_rate(bool)=True, keep_aspect(bool)=False | The scale parameter scales the video to the label size. The consistant_frame_rate parameter skips frames to keep the framerate consistant and keep_aspect keeps aspect ratio when resizing(note: It will not increase the size) |
| \_\_init\_\_ | scaled(bool), consistant_frame_rate(bool)=True, keep_aspect(bool)=False, loop(bool)=True | The scale parameter scales the video to the label size. The consistant_frame_rate parameter skips frames to keep the framerate consistant and keep_aspect keeps aspect ratio when resizing(note: It will not increase the size) |
| set_scaled | scaled(bool), keep_aspect(bool)=False | scales the video to the label size. |
| loop | loop(bool) | Sets whether the video should be looped.
| load | file_path(str) | starts loading the video in a thread. |
| set_size | size(Tuple[int, int]), keep_aspect(bool)=False | sets the size of the video frame. setting this will set scaled to `False` |
| current_duration | - | return video duration in seconds. |
Expand Down
10 changes: 3 additions & 7 deletions examples/loop_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@
from tkVideoPlayer import TkinterVideo


def loop(e):
# if the video had ended then replays the video from the beginning
tkvideo.play()


root = tk.Tk()

tkvideo = TkinterVideo(scaled=True, master=root)
tkvideo = TkinterVideo(scaled=True, master=root, loop=True)
# tkvideo.loop = True # can also set loop value after initialization
# tkvideo.loop(True) # alternative
tkvideo.load(r"sample_m4v.m4v")
tkvideo.pack(expand=True, fill="both")
tkvideo.play() # play the video
tkvideo.bind("<<Ended>>", loop) # when the video ends calls the loop function
root.mainloop()
20 changes: 18 additions & 2 deletions tkVideoPlayer/tkvideoplayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@

class TkinterVideo(tk.Label):

def __init__(self, master, scaled: bool = True, consistant_frame_rate: bool = True, keep_aspect: bool = False, *args, **kwargs):
def __init__(self, master, scaled: bool = True, consistant_frame_rate: bool = True, keep_aspect: bool = False, loop: bool = False, *args, **kwargs):
super(TkinterVideo, self).__init__(master, *args, **kwargs)

self.path = ""
self._load_thread = None

self._paused = True
self._stop = True
self._loop = loop

self.consistant_frame_rate = consistant_frame_rate # tries to keep the frame rate consistant by skipping over a few frames

Expand Down Expand Up @@ -53,6 +54,15 @@ def keep_aspect(self, keep_aspect: bool):
""" keeps the aspect ratio when resizing the image """
self._keep_aspect_ratio = keep_aspect

@property
def loop(self) -> bool:
""" restarts the video from the start when video ends """
return self._loop

@loop.setter
def loop(self, loop: bool):
self._loop = loop

def set_resampling_method(self, method: int):
""" sets the resampling method when resizing """
self._resampling_method = method
Expand Down Expand Up @@ -180,7 +190,13 @@ def _load(self, path):

# time.sleep(abs((1 / self._video_info["framerate"]) - (delta / 1000)))

except (StopIteration, av.error.EOFError, tk.TclError):
except av.error.EOFError:
if self._loop:
self.seek(0)
else:
break

except (StopIteration, tk.TclError):
break

self._frame_number = 0
Expand Down