Skip to content

Instantly share code, notes, and snippets.

@Ansh-Rathod
Last active August 24, 2024 09:22
Show Gist options
  • Save Ansh-Rathod/d08a3618d1221e86956b25c8d9da9634 to your computer and use it in GitHub Desktop.
Save Ansh-Rathod/d08a3618d1221e86956b25c8d9da9634 to your computer and use it in GitHub Desktop.
Never have those width/height not divisible by 2 or padding dimensions are lower than size errors anymore. script is in dart you can ask ai to convert.
import 'dart:convert';
import 'dart:io';
import 'dart:math';
String generateScaleCommand(
int inputWidth, int inputHeight, int originalWidth, int originalHeight) {
final aspectRatio = originalWidth / originalHeight;
bool fitToWidth = inputWidth > inputHeight;
String filter = "";
if (fitToWidth) {
var iw = (inputHeight * aspectRatio).toInt();
var ih = inputHeight;
// clamp iw and ih to the inputwidth and inputheight but keep the ratio and divide by 2
if (iw > inputWidth || ih > inputHeight) {
final newSize =
resizeBoxWithRatio(Size(iw, ih), Size(inputWidth, inputHeight));
iw = newSize.width.toInt();
ih = newSize.height.toInt();
}
iw -= (iw % 2);
ih -= (ih % 2);
filter =
"scale=$iw:$ih,pad=$inputWidth:$inputHeight:($inputWidth-iw)/2:($inputHeight-ih)/2";
} else {
var iw = inputWidth;
var ih = (inputWidth / aspectRatio).toInt();
iw -= (iw % 2);
ih -= (ih % 2);
if (iw > inputWidth || ih > inputHeight) {
final newSize =
resizeBoxWithRatio(Size(iw, ih), Size(inputWidth, inputHeight));
iw = newSize.width.toInt();
ih = newSize.height.toInt();
}
filter =
"scale=$iw:$ih,pad=$inputWidth:$inputHeight:($inputWidth-iw)/2:($inputHeight-ih)/2";
}
return filter;
}
class Size {
num width;
num height;
Size(this.width, this.height);
}
Size resizeBoxWithRatio(Size originalSize, Size maxSize) {
final maxWidth = maxSize.width;
final maxHeight = maxSize.height;
final originalWidth = originalSize.width;
final originalHeight = originalSize.height;
// Calculate the scale factors for width and height
double scaleWidth = maxWidth / originalWidth;
double scaleHeight = maxHeight / originalHeight;
// Use the smaller scale factor to ensure the box fits within the maximum dimensions
double scale = min(scaleWidth, scaleHeight);
// Calculate the new dimensions
double newWidth = (originalWidth * scale);
double newHeight = (originalHeight * scale);
return Size(newWidth, newHeight);
}
void main() async {
String inputPath = "../trailer.mp4";
// these are the original dimensions of the video file
const int originalWidth = 1920;
const int originalHeight = 1080;
int inputWidth = 500; // Replace with actual input width
int inputHeight = 600; // Replace with actual input height
String ffmpegCommand = generateScaleCommand(
inputWidth,
inputHeight,
originalWidth,
originalHeight,
);
print('FFmpeg Command: $ffmpegCommand');
final process = await Process.start("ffmpeg", [
"-i",
inputPath,
"-y",
"-ss",
"00:00:10.00",
"-t",
"00:00:20.00",
"-vf",
ffmpegCommand,
"-c:v",
"libx264",
"../output2.mp4",
]);
process.stdout.transform(utf8.decoder).listen((event) {
print(event);
});
process.stderr.transform(utf8.decoder).listen((event) {
print(event);
});
print(await process.exitCode);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment