Skip to content

Commit

Permalink
feat(server): option to transcode to original resolution (immich-app#…
Browse files Browse the repository at this point in the history
…2709)

* option to transcode to original resolution

* changed value for target res setting

* updated test, clarified scaling condition
  • Loading branch information
mertalev authored Jun 10, 2023
1 parent e369469 commit 9cdec62
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
17 changes: 17 additions & 0 deletions server/src/domain/media/media.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,23 @@ describe(MediaService.name, () => {
);
});

it('should not scale resolution if no target resolution', async () => {
mediaMock.probe.mockResolvedValue(probeStub.videoStream2160p);
configMock.load.mockResolvedValue([
{ key: SystemConfigKey.FFMPEG_TRANSCODE, value: 'all' },
{ key: SystemConfigKey.FFMPEG_TARGET_RESOLUTION, value: 'original' },
]);
await sut.handleVideoConversion({ id: assetEntityStub.video.id });
expect(mediaMock.transcode).toHaveBeenCalledWith(
'/original/path.ext',
'upload/encoded-video/user-id/asset-id.mp4',
{
outputOptions: ['-vcodec h264', '-acodec aac', '-movflags faststart', '-preset ultrafast', '-crf 23'],
twoPass: false,
},
);
});

it('should transcode with alternate scaling video is vertical', async () => {
mediaMock.probe.mockResolvedValue(probeStub.videoStreamVertical2160p);
configMock.load.mockResolvedValue([{ key: SystemConfigKey.FFMPEG_TRANSCODE, value: 'optimal' }]);
Expand Down
11 changes: 6 additions & 5 deletions server/src/domain/media/media.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ export class MediaService {
);

const allTargetsMatching = isTargetVideoCodec && isTargetAudioCodec && isTargetContainer;

const targetResolution = Number.parseInt(ffmpegConfig.targetResolution);
const isLargerThanTargetResolution = Math.min(videoStream.height, videoStream.width) > targetResolution;
const scalingEnabled = ffmpegConfig.targetResolution !== 'original';
const targetRes = Number.parseInt(ffmpegConfig.targetResolution);
const isLargerThanTargetRes = scalingEnabled && Math.min(videoStream.height, videoStream.width) > targetRes;

switch (ffmpegConfig.transcode) {
case TranscodePreset.DISABLED:
Expand All @@ -194,7 +194,7 @@ export class MediaService {
return !allTargetsMatching;

case TranscodePreset.OPTIMAL:
return !allTargetsMatching || isLargerThanTargetResolution;
return !allTargetsMatching || isLargerThanTargetRes;

default:
return false;
Expand All @@ -212,10 +212,11 @@ export class MediaService {

// video dimensions
const videoIsRotated = Math.abs(stream.rotation) === 90;
const scalingEnabled = ffmpeg.targetResolution !== 'original';
const targetResolution = Number.parseInt(ffmpeg.targetResolution);
const isVideoVertical = stream.height > stream.width || videoIsRotated;
const scaling = isVideoVertical ? `${targetResolution}:-2` : `-2:${targetResolution}`;
const shouldScale = Math.min(stream.height, stream.width) > targetResolution;
const shouldScale = scalingEnabled && Math.min(stream.height, stream.width) > targetResolution;

// video codec
const isVP9 = ffmpeg.targetVideoCodec === 'vp9';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@
{ value: '1440', text: '1440p' },
{ value: '1080', text: '1080p' },
{ value: '720', text: '720p' },
{ value: '480', text: '480p' }
{ value: '480', text: '480p' },
{ value: 'original', text: 'original' }
]}
name="resolution"
isEdited={!(ffmpegConfig.targetResolution == savedConfig.targetResolution)}
Expand Down

0 comments on commit 9cdec62

Please sign in to comment.