-
Notifications
You must be signed in to change notification settings - Fork 178
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
Video: Allow re-trimming of already trimmed video #9315
Changes from 4 commits
109716e
327805a
a76495e
ae73c38
9afe07f
b39f736
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2021 Google LLC | ||
* | ||
* 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 | ||
* | ||
* https://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. | ||
*/ | ||
|
||
/** | ||
* Converts time in H:M:S format to milliseconds. | ||
* | ||
* @param {string} time Time in HH:MM:SS or H:M:S format. | ||
* @return {number} Milliseconds. | ||
*/ | ||
function getMsFromHMS(time) { | ||
if (!time) { | ||
return 0; | ||
} | ||
const parts = time.split(':'); | ||
if (parts.length !== 3) { | ||
return 0; | ||
} | ||
const seconds = | ||
parseFloat(parts[2]) + parseInt(parts[1]) * 60 + parseInt(parts[0]) * 3600; | ||
return Math.round(1000 * seconds); | ||
} | ||
|
||
export default getMsFromHMS; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright 2021 Google LLC | ||
* | ||
* 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 | ||
* | ||
* https://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. | ||
*/ | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import getMsFromHMS from '../getMsFromHMS'; | ||
|
||
describe('getMsFromHMS', () => { | ||
it('should correctly format values resulting in 0', () => { | ||
expect(getMsFromHMS('00:00:00')).toBe(0); | ||
expect(getMsFromHMS(null)).toBe(0); | ||
expect(getMsFromHMS('foo')).toBe(0); | ||
}); | ||
|
||
it('should return correct results', () => { | ||
expect(getMsFromHMS('00:00:01')).toBe(1000); | ||
expect(getMsFromHMS('00:01:00')).toBe(60000); | ||
expect(getMsFromHMS('00:00:10.5')).toBe(10500); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,19 +25,17 @@ import { trackEvent } from '@web-stories-wp/tracking'; | |
/** | ||
* Internal dependencies | ||
*/ | ||
import { useLocalMedia, useStory } from '../../app'; | ||
import { useLocalMedia } from '../../app'; | ||
import VideoTrimContext from './videoTrimContext'; | ||
import useVideoTrimMode from './useVideoTrimMode'; | ||
import useVideoNode from './useVideoNode'; | ||
|
||
function VideoTrimProvider({ children }) { | ||
const { selectedElements } = useStory(({ state: { selectedElements } }) => ({ | ||
selectedElements, | ||
})); | ||
const { trimExistingVideo } = useLocalMedia((state) => ({ | ||
trimExistingVideo: state.actions.trimExistingVideo, | ||
})); | ||
const { isTrimMode, hasTrimMode, toggleTrimMode } = useVideoTrimMode(); | ||
const { isTrimMode, hasTrimMode, toggleTrimMode, videoData } = | ||
useVideoTrimMode(); | ||
const { | ||
hasChanged, | ||
currentTime, | ||
|
@@ -49,20 +47,24 @@ function VideoTrimProvider({ children }) { | |
setVideoNode, | ||
resetOffsets, | ||
setIsDraggingHandles, | ||
} = useVideoNode(); | ||
} = useVideoNode(videoData); | ||
|
||
const performTrim = useCallback(() => { | ||
const { resource } = selectedElements[0]; | ||
const { resource, element } = videoData; | ||
if (!resource) { | ||
return; | ||
} | ||
const lengthInSeconds = Math.round(endOffset / 1000 - startOffset / 1000); | ||
trimExistingVideo({ | ||
resource: { | ||
...resource, | ||
// Preserve the id of the source resource even though we might be trimming from a third resource | ||
id: element.resource.id, | ||
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. Can you explain this a little more. 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. Yeah, it's a bit messy, but we need both IDs in the media processing function. This ID here is the resource from which to copy the information over to the new element. This is considered the source for all data except the actual raw video. It is used in The other ID listed a few lines below, I would be good to clean this up a bit more, but I'm not really sure how to do that... 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. Different naming might be useful. 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. I reorganized it a bit in 9afe07f - does that make it clearer? I separate it between the resource and the canvas resource. The former is the actual resource where working on, where the canvas resource is the element on the canvas that should be cloned into a new element with the resulting video. |
||
length: lengthInSeconds, | ||
lengthFormatted: getVideoLengthDisplay(lengthInSeconds), | ||
}, | ||
// However, also keep the trim source id available (it might be identical) | ||
trimSourceId: resource.id, | ||
barklund marked this conversation as resolved.
Show resolved
Hide resolved
|
||
start: formatMsToHMS(startOffset), | ||
end: formatMsToHMS(endOffset), | ||
}); | ||
|
@@ -73,16 +75,11 @@ function VideoTrimProvider({ children }) { | |
end_offset: endOffset, | ||
}); | ||
toggleTrimMode(); | ||
}, [ | ||
endOffset, | ||
startOffset, | ||
trimExistingVideo, | ||
selectedElements, | ||
toggleTrimMode, | ||
]); | ||
}, [endOffset, startOffset, trimExistingVideo, toggleTrimMode, videoData]); | ||
|
||
const value = { | ||
state: { | ||
videoData, | ||
hasChanged, | ||
isTrimMode, | ||
hasTrimMode, | ||
|
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.
@sayedtaqui Flagging will need to taken in account for #9318