Repository navigation
fix: load CldVideoPlayer posters when aspectRatio is set #674
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
PaulBoye-py
wants to merge
1
commit into
main
Choose a base branch
from
fix/video-player-aspect-ratio-poster
base: main
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
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
23 changes: 23 additions & 0 deletions
23
next-cloudinary/src/components/CldVideoPlayer/getPlayerTransformation.ts
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,23 @@ | ||
| import type { CloudinaryVideoPlayerOptions } from '@cloudinary-util/types'; | ||
|
|
||
| import type { CldVideoPlayerProps } from './CldVideoPlayer.types'; | ||
|
|
||
| /** | ||
| * getPlayerTransformation | ||
| * @description getVideoPlayerOptions passes the transformation as an array ([{ quality }, ...transformation]). | ||
| * The player turns each array item into its own URL step, which leaves an `aspectRatio` alone in | ||
| * a step and breaks poster URLs (cloudinary/cloudinary-video-player#1087). When an aspectRatio is | ||
| * set and the caller didn't pass an array themselves, combine everything into a single | ||
| * transformation object. Players without an aspectRatio keep their existing URLs. | ||
| */ | ||
|
|
||
| export function getPlayerTransformation( | ||
| props: Pick<CldVideoPlayerProps, 'aspectRatio' | 'transformation'>, | ||
| transformation: CloudinaryVideoPlayerOptions['transformation'] | ||
| ): CloudinaryVideoPlayerOptions['transformation'] { | ||
| if ( props.aspectRatio && !Array.isArray(props.transformation) && Array.isArray(transformation) ) { | ||
| return Object.assign({}, ...transformation.filter(Boolean)); | ||
| } | ||
|
|
||
| return transformation; | ||
| } | ||
61 changes: 61 additions & 0 deletions
61
next-cloudinary/tests/components/getPlayerTransformation.spec.js
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,61 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { getVideoPlayerOptions } from '@cloudinary-util/url-loader'; | ||
|
|
||
| import { getPlayerTransformation } from '../../src/components/CldVideoPlayer/getPlayerTransformation'; | ||
|
|
||
| const config = { cloud: { cloudName: 'test' } }; | ||
|
|
||
| function getTransformation(props) { | ||
| const playerOptions = getVideoPlayerOptions(props, config); | ||
| return getPlayerTransformation(props, playerOptions.transformation); | ||
| } | ||
|
|
||
| describe('CldVideoPlayer', () => { | ||
| describe('getPlayerTransformation', () => { | ||
| it('should keep the transformation array when there is no aspectRatio', () => { | ||
| const props = { | ||
| src: 'turtle', | ||
| transformation: { effect: 'sepia' } | ||
| }; | ||
|
|
||
| const { transformation } = getVideoPlayerOptions(props, config); | ||
|
|
||
| expect(getTransformation(props)).toEqual(transformation); | ||
| }); | ||
|
|
||
| it('should combine the transformation into one object when there is an aspectRatio', () => { | ||
| expect(getTransformation({ | ||
| src: 'turtle', | ||
| aspectRatio: '16:9', | ||
| transformation: { effect: 'sepia' } | ||
| })).toEqual({ quality: 'auto', effect: 'sepia' }); | ||
| }); | ||
|
|
||
| it('should combine into one object when there is an aspectRatio and no transformation', () => { | ||
| expect(getTransformation({ | ||
| src: 'turtle', | ||
| aspectRatio: '16:9' | ||
| })).toEqual({ quality: 'auto' }); | ||
| }); | ||
|
|
||
| // The crop mode is passed to the player separately, not as part of the transformation | ||
| it('should combine into one object with an aspectRatio, crop mode and quality', () => { | ||
| expect(getTransformation({ | ||
| src: 'turtle', | ||
| aspectRatio: '16:9', | ||
| cropMode: 'fill', | ||
| quality: 50 | ||
| })).toEqual({ quality: 50 }); | ||
| }); | ||
|
|
||
| it('should keep the transformation array when the caller passes an array', () => { | ||
| const props = { | ||
| src: 'turtle', | ||
| aspectRatio: '16:9', | ||
| transformation: [{ effect: 'sepia' }, { width: 300 }] | ||
| }; | ||
|
|
||
| expect(getTransformation(props)).toEqual([{ quality: 'auto' }, { effect: 'sepia' }, { width: 300 }]); | ||
| }); | ||
| }); | ||
| }); |
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.
Did the browser comparison include a source whose aspect ratio is different from the aspectRatio prop (e.g. a vertical or 4:3 video with aspectRatio="16:9")? Combining ar into the same step as the player's c_limit might change the delivered video's dimensions compared to 7.0.0. Could you compare the video URL dimensions for that case?
Uh oh!
There was an error while loading. Please reload this page.
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.
Thanks for the question, @strausr .
I compared them with a vertical video (1080×1920) and a 4:3 one (1440×1080), both with aspectRatio="16:9", running the published 7.0.0 package against this PR in a 640px-wide player. I measured the delivered files: the video with ffprobe, and the poster by downloading it.
Vertical (9:16):
aspectRatio only: video 1080×1920 in both; poster 400 in 7.0.0, 222×394 with the PR;
with cropMode="fill": video 1080×608 in both; poster 700×394 in both;
with a transformation object: video 1080×1920 in both; poster 400 in 7.0.0, 222×394 with the PR.
4:3:
aspectRatio only: video 1440×1080 in both; poster 400 in 7.0.0, 525×394 with the PR;
with cropMode="fill": video 1440×810 in both; poster 700×394 in both;
with a transformation object: video 1440×1080 in both; poster 400 in 7.0.0, 525×394 with the PR.
Video: the delivered dimensions are the same as 7.0.0 in every case, and also with a 1200px player and on a 2× screen. By default, the player only adds c_limit and the width to the poster URL, not the video URL. ar_16:9 without a crop doesn't resize the video, whether it's in its own step or combined.
Poster: it now loads, and keeps the source's shape inside a 16:9 box of the width the player requests. That width is based on the player's on-screen width and the screen's pixel density: 700 here, 1300 for the same player on a 2× screen.
One difference I found is that with cropMode="fill", if the video is narrower than that requested width, the poster is now scaled up to it, because c_fill and the width are in the same step. In 7.0.0, the separate c_limit step kept it at the source size. For example, the vertical video in a 1200px player gives 1080×608 in 7.0.0 and 1200×675 with this PR. The video itself isn't affected.