Created
May 4, 2020 19:52
-
-
Save cocoabox/964e4faaec85e8c1b0ebd11541ca42b1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" | |
ATV="$DIR/atv" | |
ATV_URL="$DIR/atv-airplay-url" | |
TMP_DIR="/tmp/play-$RANDOM" | |
HTTP_SERVER="/usr/local/bin/http-server" | |
NPM="/usr/local/bin/npm" | |
if [[ ! -f "$1" ]]; then | |
echo "usage : $0 [FULL_PATH_TO_MEDIA_FILE] ['loop']" >&2 | |
exit 1 | |
fi | |
"$HTTP_SERVER" --help >/dev/null 2>&1 | |
if [[ $? -eq 127 ]]; then | |
echo "** no http-server ; trying to install" >&2 | |
"$NPM" install -g http-server | |
if [[ $? -eq 127 ]]; then | |
echo "ERROR : need npm (nodeJS)" >&2 | |
exit 1 | |
fi | |
fi | |
if [[ "$2" == "loop" ]]; then | |
LOOP=1 | |
fi | |
NAME=$(basename -- "$1") | |
EXT="${NAME##*.}" | |
HTTP_SERVER_PID="" | |
FFMPEG_PID="" | |
echo "## Temp dir = $TMP_DIR" | |
mkdir "$TMP_DIR" | |
if [[ $LOOP -eq 1 ]]; then | |
echo "## Starting ffmpeg" | |
# see : https://stackoverflow.com/questions/30846719/infinite-stream-from-a-video-file-in-a-loop | |
ffmpeg \ | |
-loglevel warning \ | |
-threads $(sysctl -a | awk '/machdep.cpu.core_count/ {print $2}') \ | |
-re \ | |
-fflags +genpts \ | |
-stream_loop -1 \ | |
-i "$1" \ | |
-hls_flags delete_segments \ | |
-c copy "$TMP_DIR/loop.m3u8" & | |
FFMPEG_PID=$! | |
echo " --> ffmpeg pid = $FFMPEG_PID" | |
else | |
echo "## Creating symlink" | |
ln -s "$1" $TMP_DIR/movie.$EXT | |
fi | |
ADDR=$(ifconfig en0 | awk '$1=="inet" {print $2}') | |
PORT="8080" | |
DIRTY=1 | |
clean_up() { | |
[[ $DIRTY -eq 0 ]] && return 0 | |
if [[ ! -z "$FFMPEG_PID" ]]; then | |
echo "## Quit ffmpeg" | |
kill -9 "$FFMPEG_PID" 2>/dev/null | |
FFMPEG_PID="" | |
sleep 1 | |
fi | |
echo "## Cleanup" | |
rm -Rf "$TMP_DIR" | |
if [[ ! -z "$HTTP_SERVER_PID" ]]; then | |
kill -9 "$HTTP_SERVER_PID" 2>/dev/null | |
HTTP_SERVER_PID="" | |
fi | |
DIRTY=0 | |
} | |
trap 'clean_up' SIGINT | |
echo "## Starting http-server" | |
"$HTTP_SERVER" \ | |
-s \ | |
-c-1 \ | |
-a "$ADDR"\ | |
-p ${PORT} \ | |
-d false \ | |
"$TMP_DIR" & | |
HTTP_SERVER_PID=$! | |
echo " --> http-server pid = $HTTP_SERVER_PID" | |
echo "## Requesting airplay" | |
if [[ $LOOP -eq 1 ]]; then | |
echo "## Looping ; press ^C to stop" | |
"$ATV_URL" "http://${ADDR}:${PORT}/loop.m3u8" | |
else | |
"$ATV_URL" "http://${ADDR}:${PORT}/movie.${EXT}" | |
fi | |
clean_up | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment