Skip to content

Commit 8af3398

Browse files
committed
added video compression function to toolset
1 parent fe7aa23 commit 8af3398

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

.config/nvim/init.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ if has('nvim')
330330
set inccommand=nosplit
331331
endif
332332
set mouse=a " for mouse to work in tmux and vim
333+
set paste
333334

334335

335336

.vimrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,6 @@ set nobackup " swap files are enough
217217
set hidden " hide buffers instead of closing them.
218218
set switchbuf=usetab,newtab,useopen
219219
"set nu " set rnu for relative numbering.
220-
set paste " only set if needed
221220
set list
222221
set showbreak=\
223222
set listchars=tab:↦-,nbsp:␣,trail:∙,extends:⟩,precedes:
@@ -230,6 +229,7 @@ set splitbelow "move preview window to below, so it doesn't move the code
230229
if g:os == "Windows"
231230
set mouse-=a
232231
endif
232+
set paste
233233

234234
" Vim Rabbit Hole Hierarchy:
235235
" ---

.zshfunctions

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,45 @@ check_not_rosetta() {
206206
fi
207207
}
208208

209+
compressvideo() {
210+
if [ $# -lt 2 ]; then
211+
echo "Usage: compress_video <input_file> <size_in_MB> [output_file]"
212+
return 1
213+
fi
214+
INPUT="$1"
215+
SIZE_MB="$2"
216+
OUTPUT="${3:-output_compressed.mp4}"
217+
MAX_ATTEMPTS=5
218+
REDUCTION_FACTOR=0.9
219+
DURATION=$(ffprobe -v error -select_streams v:0 -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$INPUT")
220+
TARGET_SIZE_BYTES=$((SIZE_MB * 1024 * 1024))
221+
AUDIO_BITRATE=128
222+
TOTAL_BITRATE=$(echo "$TARGET_SIZE_BYTES * 8 / $DURATION / 1000" | bc)
223+
224+
attempt=1
225+
while [ $attempt -le $MAX_ATTEMPTS ]; do
226+
VIDEO_BITRATE=$((TOTAL_BITRATE - AUDIO_BITRATE))
227+
if [ "$VIDEO_BITRATE" -le 0 ]; then
228+
AUDIO_BITRATE=64
229+
VIDEO_BITRATE=$((TOTAL_BITRATE - AUDIO_BITRATE))
230+
if [ "$VIDEO_BITRATE" -le 0 ]; then
231+
return 1
232+
fi
233+
fi
234+
rm -f ffmpeg2pass-0.log*
235+
ffmpeg -y -i "$INPUT" -c:v libx264 -preset medium -b:v "${VIDEO_BITRATE}k" -pass 1 -c:a aac -b:a "${AUDIO_BITRATE}k" -f mp4 /dev/null
236+
ffmpeg -i "$INPUT" -c:v libx264 -preset medium -b:v "${VIDEO_BITRATE}k" -pass 2 -c:a aac -b:a "${AUDIO_BITRATE}k" "$OUTPUT"
237+
SIZE=$(stat -c%s "$OUTPUT" 2>/dev/null || stat -f%z "$OUTPUT")
238+
if [ "$SIZE" -le "$TARGET_SIZE_BYTES" ]; then
239+
return 0
240+
else
241+
rm -f "$OUTPUT"
242+
TOTAL_BITRATE=$(echo "$TOTAL_BITRATE * $REDUCTION_FACTOR" | bc | awk '{printf "%d\n",$0}')
243+
if [ "$TOTAL_BITRATE" -le 0 ]; then
244+
return 1
245+
fi
246+
fi
247+
attempt=$((attempt + 1))
248+
done
249+
return 1
250+
}

0 commit comments

Comments
 (0)