Skip to content

Instantly share code, notes, and snippets.

@thorhop
Created October 4, 2021 11:30
Show Gist options
  • Save thorhop/c9ced5306a141035a94b97e35ddedf77 to your computer and use it in GitHub Desktop.
Save thorhop/c9ced5306a141035a94b97e35ddedf77 to your computer and use it in GitHub Desktop.
ffmpeg-update (as bundled with Vivaldi)
#!/bin/sh -eu
FFMPEG_VERSION_DEB='93.0.4577.63-0ubuntu0.18.04.1'
available () {
command -v "$1" >/dev/null 2>&1
}
available ldd && LIBC_MINOR_VERSION="$(ldd --version | head -n1 | sed -n '/^ldd .* [2-9]\./s/.*\.\([0-9]\+\)$/\1/p')"
LIBC_MINOR_VERSION="${LIBC_MINOR_VERSION:-27}"
check_glibc () {
if [ "$LIBC_MINOR_VERSION" -lt "$1" ]; then
echo "Your glibc version is too old to search for a replacement libffmpeg that supports proprietary media" >&2
exit 1
fi
}
case 'amd64' in
amd64|x86_64)
FFMPEG_USE_SNAP=0
FFMPEG_URL_DEB="https://launchpadlibrarian.net/556385725/chromium-codecs-ffmpeg-extra_${FFMPEG_VERSION_DEB}_amd64.deb"
FFMPEG_XZ_OFFSET_DEB='1153'
FFMPEG_SUM_DEB='79b56700ea9558c50a5edacaeb70298120127d2f8b32bb5e05c3876a51bd64b5'
FFMPEG_MIN_GLIBC_DEB='27'
FFMPEG_URL_SNAP='https://api.snapcraft.io/api/v1/snaps/download/XXzVIXswXKHqlUATPqGCj2w2l7BxosS8_23.snap'
FFMPEG_XZ_OFFSET_SNAP='97'
FFMPEG_LIB_OFFSET_SNAP='1'
FFMPEG_LENGTH_SNAP='19451350'
FFMPEG_SUM_SNAP='fb632265f2fecc38ac8073b6c5cdcd939c2aa5674d468ff409f5fbcd2568c876'
FFMPEG_VERSION_SNAP=103551
FFMPEG_MIN_GLIBC_SNAP='18'
check_glibc "$FFMPEG_MIN_GLIBC_SNAP"
# Use the snap if only _slightly_ old glibc
if [ "$LIBC_MINOR_VERSION" -lt "$FFMPEG_MIN_GLIBC_DEB" ]; then
FFMPEG_USE_SNAP=1
fi
;;
armhf)
FFMPEG_USE_SNAP=0
FFMPEG_URL_DEB="https://launchpadlibrarian.net/556802229/chromium-codecs-ffmpeg-extra_${FFMPEG_VERSION_DEB}_armhf.deb"
FFMPEG_XZ_OFFSET_DEB='1153'
FFMPEG_SUM_DEB='50dc605687ae7b380a6e334dcb1c05bb719db30732cd608d129227d3cc59a0af'
FFMPEG_MIN_GLIBC_DEB='27'
check_glibc "$FFMPEG_MIN_GLIBC_DEB"
;;
arm64)
FFMPEG_USE_SNAP=0
FFMPEG_URL_DEB="https://launchpadlibrarian.net/556460409/chromium-codecs-ffmpeg-extra_${FFMPEG_VERSION_DEB}_arm64.deb"
FFMPEG_XZ_OFFSET_DEB='1157'
FFMPEG_SUM_DEB='be39cf17fefd088a9c8aac5048071ddf0f5d8804172db6721a5b81fe02b341b6'
FFMPEG_MIN_GLIBC_DEB='27'
check_glibc "$FFMPEG_MIN_GLIBC_DEB"
;;
esac
if [ "${1-}" = "--system" ]; then
shift 1
fi
if [ "${1-}" = "--user" ]; then
FFMPEG_INSTALL_DIR_DEB="$HOME/.local/lib/vivaldi/media-codecs-$FFMPEG_SUM_DEB"
case 'amd64' in
amd64|x86_64) FFMPEG_INSTALL_DIR_SNAP="$HOME/.local/lib/vivaldi/media-codecs-$FFMPEG_SUM_SNAP" ;;
esac
shift 1
else
FFMPEG_INSTALL_DIR_DEB="/var/opt/vivaldi/media-codecs-$FFMPEG_SUM_DEB"
case 'amd64' in
amd64|x86_64) FFMPEG_INSTALL_DIR_SNAP="/var/opt/vivaldi/media-codecs-$FFMPEG_SUM_SNAP" ;;
esac
if [ "${USER:-}" != "root" ]; then
echo "You may need to be root (or rerun this command with sudo)" >&2
fi
fi
cleanup_files () {
# Cleanup needs to be able to handle files from earlier installs, where the
# numbered path could be different.
if ls "${FFMPEG_INSTALL_DIR_DEB%/media-codecs-*}"/media-codecs-*/libffmpeg.so >/dev/null 2>&1; then
rm -f "${FFMPEG_INSTALL_DIR_DEB%/media-codecs-*}"/media-codecs-*/libffmpeg.so
fi
if [ -d "${FFMPEG_INSTALL_DIR_DEB%/media-codecs-*}" ]; then
find "${FFMPEG_INSTALL_DIR_DEB%/media-codecs-*}" -depth -type d -empty -exec rmdir {} \;
fi
}
if [ "${1-}" = "--undo" ]; then
cleanup_files
exit
fi
if ! available sha256sum; then
echo "sha256sum is not installed; aborting" >&2
exit 1
fi
# If a suitable file already exists we do not need to do anything and
# can exit early.
case 'amd64' in
amd64|x86_64)
if [ -e "$FFMPEG_INSTALL_DIR_DEB/libffmpeg.so" ] && echo "$FFMPEG_SUM_DEB $FFMPEG_INSTALL_DIR_DEB/libffmpeg.so" | sha256sum -c >/dev/null 2>&1; then
echo "Proprietary media codecs (${FFMPEG_VERSION_DEB%-*}) was already present"
chmod -R u+rwX,go+rX-w "${FFMPEG_INSTALL_DIR_DEB%/media-codecs-*}"
exit 0
elif [ -e "$FFMPEG_INSTALL_DIR_SNAP/libffmpeg.so" ] && echo "$FFMPEG_SUM_SNAP $FFMPEG_INSTALL_DIR_SNAP/libffmpeg.so" | sha256sum -c >/dev/null 2>&1; then
echo "Proprietary media codecs ($FFMPEG_VERSION_SNAP) was already present"
chmod -R u+rwX,go+rX-w "${FFMPEG_INSTALL_DIR_SNAP%/media-codecs-*}"
exit 0
fi
;;
*)
if [ -e "$FFMPEG_INSTALL_DIR_DEB/libffmpeg.so" ] && echo "$FFMPEG_SUM_DEB $FFMPEG_INSTALL_DIR_DEB/libffmpeg.so" | sha256sum -c >/dev/null 2>&1; then
echo "Proprietary media codecs (${FFMPEG_VERSION_DEB%-*}) was already present"
chmod -R u+rwX,go+rX-w "${FFMPEG_INSTALL_DIR_DEB%/media-codecs-*}"
exit 0
fi
;;
esac
# We don't need to check certificates because we verify package contents with
# checksums. By avoiding the check we also allow for download on a distro that
# lacks an up to date certificate store (see: VB-68785)
if available wget; then
DOWNLOAD="wget -O- --no-check-certificate"
CHECK_DOWNLOAD="wget --spider"
elif available curl; then
DOWNLOAD="curl -L --insecure"
CHECK_DOWNLOAD="curl -I"
else
echo "Neither Wget nor cURL is installed; aborting" >&2
exit 1
fi
# Remove any previous version before installing the new one
cleanup_files
# Check the download URL for the .deb is (still) valid
case 'amd64' in
amd64|x86_64)
if [ "$FFMPEG_USE_SNAP" = '0' ] && ! $CHECK_DOWNLOAD "$FFMPEG_URL_DEB" 2>&1 | grep -q 'HTTP.*200'; then
FFMPEG_USE_SNAP='1'
fi
;;
*)
if ! $CHECK_DOWNLOAD "$FFMPEG_URL_DEB" 2>&1 | grep -q 'HTTP.*200'; then
echo "Cannot locate a suitable library to provide support for proprietary media." >&2
exit 1
fi
;;
esac
# Fetch and extract libffmpeg
if [ "$FFMPEG_USE_SNAP" = '1' ]; then
mkdir -p "$FFMPEG_INSTALL_DIR_SNAP"
# We hide the download progress here because wget/curl will complain since
# they are prevented from completing their download. This may confuse
# the user, causing them to suspect something has gone wrong.
$DOWNLOAD "$FFMPEG_URL_SNAP" 2>/dev/null | tail -c+"$FFMPEG_XZ_OFFSET_SNAP" | xz -d | tail -c+"$FFMPEG_LIB_OFFSET_SNAP" | head -c"$FFMPEG_LENGTH_SNAP" > "$FFMPEG_INSTALL_DIR_SNAP/libffmpeg.so" ||:
chmod -R u+rwX,go+rX-w "${FFMPEG_INSTALL_DIR_SNAP%/media-codecs-*}"
if ! echo "$FFMPEG_SUM_SNAP $FFMPEG_INSTALL_DIR_SNAP/libffmpeg.so" | sha256sum -c >/dev/null 2>&1; then
echo "The extracted libffmpeg.so does not match the expected sha256sum; aborting" >&2
cleanup_files
exit 1
fi
echo "Proprietary media codecs ($FFMPEG_VERSION_SNAP) has been installed (PLEASE RESTART VIVALDI)"
else
mkdir -p "$FFMPEG_INSTALL_DIR_DEB"
$DOWNLOAD "$FFMPEG_URL_DEB" | tail -c+"$FFMPEG_XZ_OFFSET_DEB" | xz -d | tar fOx - ./usr/lib/chromium-browser/libffmpeg.so > "$FFMPEG_INSTALL_DIR_DEB/libffmpeg.so" ||:
chmod -R u+rwX,go+rX-w "${FFMPEG_INSTALL_DIR_DEB%/media-codecs-*}"
if ! echo "$FFMPEG_SUM_DEB $FFMPEG_INSTALL_DIR_DEB/libffmpeg.so" | sha256sum -c >/dev/null 2>&1; then
echo "The extracted libffmpeg.so does not match the expected sha256sum; aborting" >&2
cleanup_files
exit 1
fi
echo "Proprietary media codecs (${FFMPEG_VERSION_DEB%-*}) has been installed (PLEASE RESTART VIVALDI)"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment