-
Notifications
You must be signed in to change notification settings - Fork 7
Added subtitles plugin #2
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
gvamshi-metrological
wants to merge
18
commits into
Metrological:master
Choose a base branch
from
gvamshi-metrological:master
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 1 commit
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
afcbbaa
Added subtitles plugin
gvamshi-metrological ccc62f6
Addressed code review comments
gvamshi-metrological 9a52ced
Integrated subtitles into videoPlayer
gvamshi-metrological 22fca8c
Merge branch 'Metrological:master' into master
gvamshi-metrological 2cc68a7
Merge branch 'master' of github.com:gvamshi-metrological/metrological…
gvamshi-metrological 5d49f22
Create test.yml
gvamshi-metrological 4f89744
Adding git hub actions to master
gvamshi-metrological b235980
Merge branch 'master' of github.com:gvamshi-metrological/metrological…
gvamshi-metrological a113ab8
Integrated subtitles into videoPlayer (#1)
gvamshi-metrological b095094
Removed unused testURl in jest.config
gvamshi-metrological be698d7
Fixed subtitle plugin issues
gvamshi-metrological 99e2730
Addressed review comments
gvamshi-metrological fcaa26c
Addressed review comments (#2)
gvamshi-metrological 3b77e58
Renamed subtitles as subtitlesParser
gvamshi-metrological b7b79ec
Handling empty subtitles
gvamshi-metrological e751fd8
Addressed code review comments
gvamshi-metrological 191d353
Addressed code review comments
gvamshi-metrological a8449ee
updated docs and removed comments
gvamshi-metrological 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| * If not stated otherwise in this file or this component's LICENSE file the | ||
| * following copyright and licenses apply: | ||
| * | ||
| * Copyright 2020 Metrological | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the License); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| export default class SubtitleAsset { | ||
| constructor(obj) { | ||
| this._start = obj.start // start time to show subtitle in sec | ||
| this._end = obj.end // end time of showing subtitle in sec | ||
| this._payload = obj.payload ? obj.payload.replace(/<(.*?)>/g, '') : '' // Remove <v- >, etc tags in subtitle text | ||
| } | ||
|
|
||
| get start() { | ||
| return this._start | ||
| } | ||
|
|
||
| get end() { | ||
| return this._end | ||
| } | ||
|
|
||
| get payload() { | ||
| return this._payload | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| /* | ||
| * If not stated otherwise in this file or this component's LICENSE file the | ||
| * following copyright and licenses apply: | ||
| * | ||
| * Copyright 2020 Metrological | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the License); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import SubtitleAsset from './SubtitleAsset.js' | ||
| export default class Subtitles { | ||
| // @ params url: subtitle file URL | ||
| // @return parsed subtitles as list of objects | ||
| // also stores parsed data | ||
| static fetchAndParseSubs(url) { | ||
| return fetch(url) | ||
| .then(data => data.text()) | ||
| .then(subtitleData => { | ||
| this.clearCurrentSubtitle() | ||
| return this.parseSubtitles(subtitleData) | ||
| }) | ||
|
robbertvancaem marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| // clears stored subtitles data | ||
| static clearCurrentSubtitle() { | ||
| this._currentSubtitle = null | ||
| this._nextSubtitle = null | ||
| } | ||
|
|
||
| // @params timeIndex: time as seconds | ||
| // @return subtitle as text at passed timeIndex | ||
| static getSubtitleByTimeIndex(timeIndex) { | ||
| let self = this | ||
|
gvamshi-metrological marked this conversation as resolved.
Outdated
|
||
| if ( | ||
| this._captions && | ||
| this._captions.length && | ||
| this._currentSubtitle && | ||
| this._nextSubtitle && | ||
| Number(timeIndex.toFixed(0)) < Number(this._nextSubtitle.end.toFixed(0)) && | ||
| Number(timeIndex.toFixed(0)) >= Number(this._currentSubtitle.start.toFixed(0)) | ||
| ) { | ||
| if ( | ||
| Number(timeIndex.toFixed(0)) >= Number(this._currentSubtitle.start.toFixed(0)) && | ||
| Number(timeIndex.toFixed(0)) < Number(this._currentSubtitle.end.toFixed(0)) | ||
| ) { | ||
| return this._currentSubtitle.payload | ||
| } else if ( | ||
| Number(timeIndex.toFixed(0)) >= Number(this._nextSubtitle.start.toFixed(0)) && | ||
| Number(timeIndex.toFixed(0)) < Number(this._nextSubtitle.end.toFixed(0)) | ||
| ) { | ||
| return this._nextSubtitle.payload | ||
| } else { | ||
| return '' | ||
| } | ||
| } else { | ||
| updateSubtitles() | ||
| } | ||
|
|
||
| function updateSubtitles() { | ||
| // updates current and next subtitle text values | ||
| if (self._captions && self._captions.length) { | ||
| if ( | ||
| Number(timeIndex.toFixed(0)) <= | ||
| Number(self._captions[self._captions.length - 1].start.toFixed(0)) | ||
| ) { | ||
| if (Number(timeIndex.toFixed(0)) < Number(self._captions[0].end.toFixed(0))) { | ||
| if (self._captions[1] && self._captions[1].payload) { | ||
| self._nextSubtitle = self._captions[1] | ||
| } | ||
| self._currentSubtitle = self._captions[0] | ||
| } else { | ||
| for (let i = 0; i < self._captions.length; i++) { | ||
| if (Number(self._captions[i].start.toFixed(0)) >= Number(timeIndex.toFixed(0))) { | ||
| self._captions[i + 1] && self._captions[i + 1].payload | ||
| ? (self._nextSubtitle = self._captions[i + 1]) | ||
| : { payload: '' } | ||
| self._currentSubtitle = self._captions[i] | ||
| break | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // parses subtitle file and returns list of time, text objects | ||
| static parseSubtitles(plainSub) { | ||
| let linesArray = plainSub | ||
| .trim() | ||
| .replace('\r\n', '\n') | ||
| .split(/[\r\n]/) | ||
| .map(line => { | ||
| return line.trim() | ||
| }) | ||
| let cues = [] | ||
| let start = null | ||
| let end = null | ||
| let payload = null | ||
| let lines = linesArray.filter(item => item !== '' && isNaN(item)) | ||
| linesArray = [] | ||
| for (let i = 0; i < lines.length; i++) { | ||
| if (lines[i].indexOf('-->') >= 0) { | ||
| let splitted = lines[i].split(/[ \t]+-->[ \t]+/) | ||
|
|
||
| start = Subtitles.parseTimeStamp(splitted[0]) | ||
| end = Subtitles.parseTimeStamp(splitted[1]) | ||
| } else if (lines[i] !== '') { | ||
| if (start && end) { | ||
| if (i + 1 < lines.length && lines[i + 1].indexOf('-->') >= 0) { | ||
| let cue = new SubtitleAsset({ | ||
| start, | ||
| end, | ||
| payload: payload ? payload + ' ' + lines[i] : lines[i], | ||
| }) | ||
|
gvamshi-metrological marked this conversation as resolved.
Outdated
|
||
| cues.push(cue) | ||
| start = null | ||
| end = null | ||
| payload = null | ||
| } else { | ||
| payload = payload ? payload + ' ' + lines[i] : lines[i] | ||
| } | ||
| } | ||
| } else if (start && end) { | ||
| if (payload == null) { | ||
| payload = lines[i] | ||
| } else { | ||
| payload += ' ' + lines[i] | ||
| } | ||
| } | ||
| } | ||
| if (start && end) { | ||
| let match = /<(.*?)>/g | ||
| if (payload) { | ||
| payload.replace(match, '') | ||
| } | ||
| let cue = new SubtitleAsset({ start, end, payload }) | ||
| cues.push(cue) | ||
| } | ||
| this._captions = cues | ||
| return this._captions | ||
| } | ||
|
|
||
| // parses timestamp in subtitle file into seconds | ||
| static parseTimeStamp(s) { | ||
| let match = s.match(/^(?:([0-9]+):)?([0-5][0-9]):([0-5][0-9](?:[.,][0-9]{0,3})?)/) | ||
|
gvamshi-metrological marked this conversation as resolved.
Outdated
|
||
|
|
||
| let hours = parseInt(match[1] || '0', 10) | ||
|
gvamshi-metrological marked this conversation as resolved.
Outdated
|
||
| let minutes = parseInt(match[2], 10) | ||
| let seconds = parseFloat(match[3].replace(',', '.')) | ||
| return seconds + 60 * minutes + 60 * 60 * hours | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.