Skip to content

Instantly share code, notes, and snippets.

@jaekookang
Last active June 10, 2022 14:30
Show Gist options
  • Save jaekookang/6a5829e00f38ed01da224076ca4960d9 to your computer and use it in GitHub Desktop.
Save jaekookang/6a5829e00f38ed01da224076ca4960d9 to your computer and use it in GitHub Desktop.
ffmpeg custom shell functions
# ffmpeg custom utilities
#
# To use:
# source ffmpeg_utils.sh
#
# URL: https://gist.github.com/jaekookang/6a5829e00f38ed01da224076ca4960d9
#
# 2021-11-25 first created jaekoo kang
function ffmpeg_mkv_mp4() {
# Convert mkv to mp4
#
# - Usage: ffmpeg_mkv_mp4 INPUT.mkv outOUTPUTput.mp4
#
# - Command: ffmpeg -i INPUT.mkv -map 0 OUTPUT.mp4
ffmpeg -i $1 -map 0 $2
}
function ffmpeg_mka_mp3() {
# Convert mka to mp3
#
# - Usage: ffmpeg_mka_mp3 INPUT.mka OUTPUT.mp3
#
# - Command: ffmpeg -i INPUT.mka OUTPUT.mp3
ffmpeg -i $1 $2
}
function ffmpeg_mka_wav() {
# Convert mka to mp3
#
# - Usage: ffmpeg_mka_wav INPUT.mka OUTPUT.wav
#
# - Command: ffmpeg -i INPUT.mka OUTPUT.wav
ffmpeg -i $1 $2
}
function ffmpeg_resize_width() {
# Specify the Width To Retain the Aspect Ratio
#
# - Usage: ffmpeg_resize_widdth INPUT.mp4 OUTPUT.mp4 320
#
# - Command: ffmpeg -i input.mp4 -vf scale=320:-1 output.mp4
#
# - Note:
# - $1: input file
# - $2: output file
# - $3: target width (int)
ffmpeg -i $1 -vf scale=$3:-1 $2
}
function ffmpeg_resize_height() {
# Specify the Height To Retain the Aspect Ratio
#
# - Usage: ffmpeg_resize_height INPUT.mp4 OUTPUT.mp4 320
#
# - Usage: ffmpeg -i input.mp4 -vf scale=-1:720 output.mp4
#
# - Note:
# - $1: input file
# - $2: output file
# - $3: target width (int)
ffmpeg -i $1 -vf scale=-1:$3 $2
}
function ffmpeg_reduce() {
# Reduce mp4 file size
#
# - Usage: ffmpeg_reduce INPUT.mp4 OUTPUT.mp4
#
# - Command: ffmpeg -i INPUT.mp4 -vcodec h264 -acodec mp3 -crf 28 OUTPUT.mp4
#
# - Note:
# - both video and audio stream are assumed
ffmpeg -i $1 -vcodec h264 -c:a copy -crf 28 $2
}
function ffmpeg_extract_audio() {
# Extract audio stream from mp4 file
#
# - Usage: ffmpeg_extract_audio INPUT.mp4 OUTPUT.mp3
#
# - Command: ffmpeg -i INPUT.mp4 -q:a 0 -map OUTPUT.mp3
ffmpeg -i $1 -q:a 0 $2
}
function ffmpeg_combine_video_audio() {
# Combine/merge video with audio
#
# - Usage: ffmpeg_combine_video_audio VIDEO.mp4 AUDIO.mp3 OUTPUT.mp4
#
# - Command: ffmpeg -i video.mp4 -i audio.wav -c:v copy -c:a aac output.mp4
ffmpeg -i $1 -i $2 -c:v copy -c:a aac $3
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment