Post #2176276
2026-02-23 05:07 UTC
The version I have was copied from stackoverflow. It doesn't work very well, it makes a rough estimate to get the video file size under the set value. As an example
```bash
resize video.mp4 10
```
Which then resizes the video to 10 megabytes if possible.
::: spoiler resize.sh code
```bash
file=$1
target_size_mb=$2 # target size in MB
target_size=$(( $target_size_mb * 1000 * 1000 * 8 )) # target size in bits
length=`ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$file"`
length_round_up=$(( ${length%.*} + 1 ))
total_bitrate=$(( $target_size / $length_round_up ))
audio_bitrate=$(( 128 * 1000 )) # 128k bit rate
video_bitrate=$(( $total_bitrate - $audio_bitrate ))
ffmpeg -i "$file" -b:v $video_bitrate -maxrate:v $video_bitrate -bufsize:v $(( $target_size / 20 )) -b:a $audio_bitrate "${file}-${target_size_mb}mb.mp4"
```
:::
I'll probably replace it eventually.
Replies (1)
-
@db2@lemmy.world 2026-02-23 05:23
Definitely not the same lol Mine uses ffmpeg to change the resolution, it doesn't so much care about file sizes. It could be a one-liner if you only ever feed it a single file to manipulate..