Skip to content

Commit

Permalink
Limit the maximum bitrate of video previews
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmz committed Jan 29, 2025
1 parent d04ed16 commit 57108d8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
2 changes: 2 additions & 0 deletions app/support/media-files/detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export async function detectMediaType(
vCodec: videoStream.codec_name,
aCodec: audioStream?.codec_name,
duration: parseFloat(format.duration),
bitrate: parseInt(format.bit_rate),
width,
height,
h264info,
Expand Down Expand Up @@ -146,6 +147,7 @@ async function detectAnimatedImage(
width: videoStream.width!,
height: videoStream.height!,
duration: parseFloat(format.duration),
bitrate: parseInt(format.bit_rate),
isAnimatedImage: true,
};
}
Expand Down
9 changes: 8 additions & 1 deletion app/support/media-files/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,9 @@ async function processVideo(
stillFile,
);

// Bitrate per pixel of the original video
const originalBpp = info.bitrate / (info.width * info.height);

for (const { variant, width, height } of previewSizes) {
if (variant === maxVariant) {
if (!canUseOriginalVideo) {
Expand All @@ -384,13 +387,17 @@ async function processVideo(
targetFile,
);
} else {
const bitrateK = Math.round((1.5 * (originalBpp * (width * height))) / 1000);
commands.push(
['-map', `[${variant}out]`],
['-c:v', 'libx264'],
['-preset', 'slow'],
['-profile:v', 'high'],
['-crf', '23'],
['-g', '60'], // For better seeking performance
// For better seeking performance
['-g', '60'],
// Limit the maximum bitrate
info.isAnimatedImage ? [] : ['-maxrate', `${bitrateK}k`, '-bufsize', `${2 * bitrateK}k`],
['-pix_fmt', 'yuv420p'],
...commonCommands,
targetFile,
Expand Down
8 changes: 7 additions & 1 deletion app/support/media-files/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export type MediaInfoVideo = {
aCodec?: string;
isAnimatedImage?: true;
h264info?: H264Info;
bitrate: number;
} & MediaInfoVisual &
MediaInfoPlayable &
MediaInfoCommon;
Expand Down Expand Up @@ -109,7 +110,12 @@ export type AvcStream = Stream & {
};

export type FfprobeResult = {
format: { format_name: string; duration: string; tags?: Record<string, string> };
format: {
format_name: string;
duration: string;
bit_rate: string;
tags?: Record<string, string>;
};
streams: Stream[];
};

Expand Down
6 changes: 5 additions & 1 deletion test/fixtures/media-files/file-info.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"height": 256,
"duration": 7.6,
"isAnimatedImage": true,
"extension": "gif"
"extension": "gif",
"bitrate": 391272
}
},
{
Expand Down Expand Up @@ -77,6 +78,7 @@
"width": 1280,
"height": 720,
"duration": 5.005,
"bitrate": 1711589,
"tags": {
"title": "Kreuz-Polka",
"major_brand": "isom",
Expand All @@ -98,6 +100,7 @@
"width": 1280,
"height": 720,
"duration": 4.993,
"bitrate": 2942024,
"extension": "ogv"
}
},
Expand All @@ -111,6 +114,7 @@
"width": 1280,
"height": 720,
"duration": 5.237,
"bitrate": 1472789,
"tags": { "encoder": "Lavf58.29.100" },
"extension": "wmv"

Expand Down

0 comments on commit 57108d8

Please sign in to comment.