Skip to content

Instantly share code, notes, and snippets.

@vitalfadeev
Created March 2, 2025 08:14
Show Gist options
  • Select an option

  • Save vitalfadeev/6e449d7bc31acbb320ae9f3ea46a0445 to your computer and use it in GitHub Desktop.

Select an option

Save vitalfadeev/6e449d7bc31acbb320ae9f3ea46a0445 to your computer and use it in GitHub Desktop.
Convert video to small size
#!/bin/sh
# Usage: convert-to-_-mp4.sh 320 28 $1
#SIZE=480
#QUAL=28
#NAME=MOVA0023.avi
SIZE=$1
QUAL=$2 # 18..28 18-best 28-small
NAME=$3
xterm -e \
ffmpeg \
-i "$NAME" \
-vf yadif,format=yuv420p \
-vf scale=-1:$SIZE:force_original_aspect_ratio=decrease:force_divisible_by=2 \
-force_key_frames "expr:gte(t,n_forced/2)" \
-c:v libx264 \
-crf $QUAL \
-bf 2 \
-c:a aac \
-q:a 1 \
-ac 2 \
-ar 48000 \
-use_editlist 0 \
-movflags +faststart \
"$NAME.$QUAL-$SIZE.mp4"
# -crf 18 \
# -b:v 512k \
# -vf specifies video filters
# yadif will deinterlace videos if they're interlaced.
# format=yuv420p will produce pixel format with 4:2:0 chroma subsampling.
# -force_key_frames "expr:gte(t,n_forced/2)" will place keyframes every half-second, so that will be the GOP size.
# -c:v libx264 will use the x264 encoder to produce a H264 video stream.
# -crf 18 will produce a visually lossless file. Better than setting a bitrate manually.
# -bf 2 will limit consecutive B-frames to 2
# -c:a aac will use the native encoder to produce an AAC audio stream.
# -q:a 1 sets the highest quality for the audio. Better than setting a bitrate manually.
# -ac 2 rematrixes audio to stereo.
# -ar 48000 resamples audio to 48000 Hz.
# -use_editlist 0 avoids writing edit lists.
# -movflags +faststart places moov atom/box at front of the output file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment