Skip to content

Instantly share code, notes, and snippets.

@sirlaurie
Last active March 4, 2024 16:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sirlaurie/212bdca2549af15e54c44684f708133c to your computer and use it in GitHub Desktop.
Save sirlaurie/212bdca2549af15e54c44684f708133c to your computer and use it in GitHub Desktop.
a script for building ffmpeg and mpv with capable playing dolby vision

适用于macOS的ffmpeg和mpv编译脚本. mac的硬件是支持播放Dolby Vision格式的视频的, 默认只有quicktime可以正确进行色彩映射, 由于quicktime支持的视频格式少得可怜, 所以用了这个脚本开启libplacebo, 以支持在mpv上播放Dolby Vision. 支持Apple Silicon和Intel平台

#!/usr/bin/env zsh
# just for macOS
#
# The default options for installing ffmpeg and mpv via brew do not include the activation of libplacebo.
# As a result, when playing videos in Dolby Vision, the color mapping is not accurate. To resolve this issue,
# a script is used to compile ffmpeg with libplacebo activated, followed by the building of mpv.
# Extra: build mpv bundle, with jxl
if [ "$(uname -s)" != "Darwin" ]; then
echo "Only support macOS now."
exit 1
fi
# Colors
GREEN='\033[0;32m'
WHITE='\033[1;37m'
GRAY='\033[37;40m'
NC='\033[0m'
# Check if Xcode installed for apple silicon
# if [ "$(arch)" == "arm64" && ! -d "/Applications/Xcode.app" ]; then
# echo "${WHITE}If you build for Apple Silicon, it is necessary to have Xcode installed to build mpv, as CLT alone is insufficient. Therefore, kindly install Xcode from the App Store before proceeding.${NC}"
# exit 1
# fi
# Install homebrew if not found
if ! command -v brew &> /dev/null; then
echo -n "${GRAY}brew is currently not found on your system. installing ... ${NC}"
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" &> /dev/null && echo "${GREEN}Done${NC}"
fi
# source "$ZDOTDIR/.zshrc" &> /dev/null || source "$HOME/.zshrc" &> /dev/null || source "$HOME/.zshenv" &> /dev/null
source "$ZDOTDIR/.zshrc" || source "$HOME/.zshrc" || source "$HOME/.zshenv"
export HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK
if [ "$(arch)" = "x86_64" ]; then
echo -n "${GRAY}installing yasm for building ffmpeg ... ${NC}"
brew install yasm &> /dev/null && echo "${GREEN}Done${NC}"
fi
# Create working directory
WORKDIR="$HOME/Downloads"
if [ ! -e $WORKDIR ]; then
mkdir -p "$WORKDIR"
fi
cd "$WORKDIR"
# install jq to calculate intersection dependencies of ffmpeg and mpv
if ! command -v jq &> /dev/null; then
echo -n "${GRAY}install jq to calculate intersection dependencies of ffmpeg and mpv ... ${NC}"
brew install jq &> /dev/null && echo "${GREEN}Done${NC}"
fi
# install dependencies only for ffmpeg and mpv
ffmpegdeps=$(brew info --json ffmpeg | jq -r '.[].dependencies | join(" ")')
mpvdeps=$(brew info --json mpv | jq -r '.[].dependencies | join(" ")')
deps=$(
cat <<EOF
$ffmpegdeps $mpvdeps libplacebo pkg-config
EOF
)
dependencies=$(echo $deps | sed 's/ffmpeg //')
installed=$(brew list -1 --formula)
# This is the only correct method to divide a lengthy string into an array with space as a separator for macOS.
# NOT arr=($var) or IFS=" " read -ra arr <<< $var
depsArr=(${(s: :)dependencies})
echo "Checking the dependencies"
for dep in "${depsArr[@]}"; do
if echo "$installed" | grep -q "$dep"; then
echo "${GREEN}$dep is already installed.${NC}"
else
echo -n "${GRAY}Installing $dep ... ${NC}"
errvar=$(brew install $dep > /dev/null) && echo "${GREEN} Done${NC}" || { echo "${WHITE} failed.${NC}"; echo $errvar }
fi
done
# Regardless of the path I set for the SRT, I consistently encounter an error while compiling FFmpeg with the '--enable-libsrt' flag. Consequently, I have decided to abandon the flag.
# export PKG_CONFIG_PATH="$(brew --prefix)/opt/srt/lib/pkgconfig:$PKG_CONFIG_PATH"
# fetch ffmpeg and mpv source code
echo "${WHITE}Clone ffmpeg ...${NC}"
if [ -d "$WORKDIR/ffmpeg" ]; then
cd "$WORKDIR/ffmpeg"
if [ "$(git config --get remote.origin.url)" = "https://git.ffmpeg.org/ffmpeg.git" ]; then
git pull
else
echo "${WHITE}$WORKDIR/ffmpeg is not official repo, please rename or remove it.${NC}" && exit 1
fi
else
git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg || exit 1
fi
cd "${WORKDIR}"
echo "${WHITE}Clone mpv ...${NC}"
if [ -d "$WORKDIR/mpv" ]; then
cd "$WORKDIR/mpv"
if [ "$(git config --get remote.origin.url)" = "https://github.com/mpv-player/mpv.git" ]; then
git pull
else
echo "${WHITE}$WORKDIR/mpv is not official repo, please rename or remove it.${NC}" && exit 1
fi
else
git clone https://github.com/mpv-player/mpv.git mpv || exit 1
fi
# # compile ffmpeg
echo "Start compiling ffmpeg ..."
cd "$WORKDIR/ffmpeg"
# 获取Clang编译器的构建版本
clang_build_version=$(clang --version | grep -oE '[0-9]+' | head -1)
# check clang version
if [ "$clang_build_version" -ge 15 ]; then
# add -Wl,-ld_classic to ldflags
export LDFLAGS="$LDFLAGS -Wl,-ld_classic"
fi
if [ -e "ffbuild/config.mak" ]; then
sudo make clean
fi
./configure --prefix="$(brew --prefix)" --cc=clang --arch=$(arch) --extra-cflags=-I$(brew --prefix)/include --extra-ldflags=-L$(brew --prefix)/lib --enable-shared --enable-pthreads --enable-version3 --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libplacebo --enable-libjxl --enable-libaom --enable-libaribb24 --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librist --enable-librubberband --enable-libsnappy --enable-libsvtav1 --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox --enable-neon --enable-vapoursynth || exit 1
make -j$(sysctl -n hw.physicalcpu) && sudo make install || exit 1
echo "${GREEN}Done${NC}"
if ! command -v meson &> /dev/null; then
echo -n "Installing meson to build mpv ..."
brew install meson &> /dev/null && echo "${GREEN}Done${NC}"
fi
# # compile mpv
echo "Start compiling mpv ..."
cd "$WORKDIR/mpv"
sed -i -e 's/UNKNOWN/1/g' "$WORKDIR/mpv/VERSION"
rm -rf "$WORKDIR/mpv/build"
meson setup build || exit 1
meson compile -j$(sysctl -n hw.physicalcpu) -C build && sudo meson install -C build || exit 1
./TOOLS/osxbundle.py -s build/mpv || exit 1
# fix crash on Sonoma
rm build/mpv.app/Contents/MacOS/mpv-bundle
mv build/mpv.app/Contents/MacOS/mpv build/mpv.app/Contents/MacOS/mpv-bundle
ln -s mpv-bundle build/mpv.app/Contents/MacOS/mpv
codesign --force --deep -s - build/mpv.app
if [ -d "/Applications/mpv.app" ]; then
rm -rf /Applications/mpv.app
fi
test -d build/mpv.app && mv build/mpv.app /Applications || exit 1
git restore "$WORKDIR/mpv/VERSION"
rm "$WORKDIR/mpv/VERSION-e"
echo "${GREEN}All Done! ffmpeg is located in ${WHITE}$(brew --prefix)/bin/ffmpeg ${GREEN}and mpv is ${WHITE}$(brew --prefix)/bin/mpv ${GREEN}alone with a bundle is ${WHITE}/Applications/mpv.app${NC}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment