-
Notifications
You must be signed in to change notification settings - Fork 46
Add timestamps class #65
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
Open
tkarabela
wants to merge
12
commits into
master
Choose a base branch
from
Add-timestamps-class
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
01e202e
Add timestamps class
moi15moi f9ea3c5
Remove format in the method frames_to_ms
moi15moi 933e7fa
[Timestamps] Correct set numerator for from_fps
moi15moi 7a9a010
Update __init__.py
moi15moi 05cae97
[timestamps] Format with Black
moi15moi e20a23c
Add test for timestamps
moi15moi d848757
Add data timestamps file
moi15moi aab9981
[Timestamps] Clean import
moi15moi 7b90aa4
Update shift and make_time with new class Timestamps
moi15moi 0bc224c
Add video test for timestamps
tkarabela e93c453
Timestamps fixes
tkarabela f637db8
Timestamps - fix mypy and Python 3.7 support
tkarabela File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
from collections import namedtuple | ||
from numbers import Real | ||
import re | ||
from typing import Optional, List, Tuple, Sequence | ||
from typing import Optional, Tuple, Sequence, Union, cast | ||
from pysubs2.common import IntOrFloat | ||
from .timestamps import Timestamps, TimeType | ||
|
||
#: Pattern that matches both SubStation and SubRip timestamps. | ||
TIMESTAMP = re.compile(r"(\d{1,2}):(\d{1,2}):(\d{1,2})[.,](\d{1,3})") | ||
|
@@ -13,7 +15,7 @@ | |
|
||
|
||
def make_time(h: IntOrFloat=0, m: IntOrFloat=0, s: IntOrFloat=0, ms: IntOrFloat=0, | ||
frames: Optional[int]=None, fps: Optional[float]=None): | ||
frames: Optional[int]=None, fps: Optional[Union[Real,Timestamps]]=None, time_type: Optional[TimeType] = None): | ||
""" | ||
Convert time to milliseconds. | ||
|
||
|
@@ -30,10 +32,15 @@ def make_time(h: IntOrFloat=0, m: IntOrFloat=0, s: IntOrFloat=0, ms: IntOrFloat= | |
2000 | ||
|
||
""" | ||
if frames is None and fps is None: | ||
if frames is None and fps is None and time_type is None: | ||
return times_to_ms(h, m, s, ms) | ||
elif frames is not None and fps is not None: | ||
return frames_to_ms(frames, fps) | ||
elif frames is not None and fps is not None and time_type is not None: | ||
if isinstance(fps, Real): | ||
timestamps = Timestamps.from_fps(fps) | ||
elif isinstance(fps, Timestamps): | ||
timestamps = fps | ||
|
||
return timestamps.frames_to_ms(frames, time_type) | ||
else: | ||
raise ValueError("Both fps and frames must be specified") | ||
|
||
|
@@ -85,43 +92,37 @@ def times_to_ms(h: IntOrFloat=0, m: IntOrFloat=0, s: IntOrFloat=0, ms: IntOrFloa | |
def frames_to_ms(frames: int, fps: float) -> int: | ||
""" | ||
Convert frame-based duration to milliseconds. | ||
|
||
Arguments: | ||
frames: Number of frames (should be int). | ||
fps: Framerate (must be a positive number, eg. 23.976). | ||
|
||
Returns: | ||
Number of milliseconds (rounded to int). | ||
|
||
Raises: | ||
ValueError: fps was negative or zero. | ||
|
||
""" | ||
if fps <= 0: | ||
raise ValueError(f"Framerate must be a positive number ({fps}).") | ||
|
||
return int(round(frames * (1000 / fps))) | ||
""" | ||
return Timestamps.from_fps(cast(Real, fps)).frames_to_ms(frames) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to add the parameter to precise the TimeType |
||
|
||
|
||
def ms_to_frames(ms: IntOrFloat, fps: float) -> int: | ||
""" | ||
Convert milliseconds to number of frames. | ||
|
||
Arguments: | ||
ms: Number of milliseconds (may be int, float or other numeric class). | ||
fps: Framerate (must be a positive number, eg. 23.976). | ||
|
||
Returns: | ||
Number of frames (int). | ||
|
||
Raises: | ||
ValueError: fps was negative or zero. | ||
|
||
""" | ||
if fps <= 0: | ||
raise ValueError(f"Framerate must be a positive number ({fps}).") | ||
|
||
return int(round((ms / 1000) * fps)) | ||
""" | ||
return Timestamps.from_fps(cast(Real, fps)).ms_to_frames(int(ms)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to add the parameter to precise the TimeType |
||
|
||
|
||
def ms_to_times(ms: IntOrFloat) -> Tuple[int, int, int, int]: | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment need to be remove