Last active
October 20, 2020 10:30
-
-
Save rekyuu/a8b2530a95bcb832d5dd851098ec0022 to your computer and use it in GitHub Desktop.
Livestreaming Script
This file contains hidden or 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/sh | |
# Usage: $ sh chika.sh -v TARGET_PATH [-i] [-t START_TIME] [-e EPISODE_NO] [-a AUDIO_TRACK] [-s SUBTITLE_TRACK] [-f] | |
# -v TARGET_PATH: Direct path to target file or directory to stream from. | |
# -i: Provide info about the file. | |
# -d: Run in debug mode, which prevents the stream from running and the playlist from being deleted. | |
# -t START_TIME: Start from specified time (as HH:MM:SS.MS). | |
# -e EPISODE_NO: Start from an episode number when a directory is provided. | |
# -a AUDIO_TRACK: Specify an audio track to use. | |
# -s SUBTITLE_TRACK: Use subs embedded in the video file. Optionally specify which subtitle track to use. | |
# -f: Force subtitle style. | |
# | |
# If TARGET_PATH is a directory, all files in the directory will be played. | |
# | |
# Note: Using -t and -s together will not work. | |
# The video will start at the specified time, but the subs will always burn in at 0 and will be offset. | |
# Specify streaming settings. | |
AUDIO_BITRATE=320k | |
VIDEO_BITRATE=3000k | |
KEYFRAME_INTERVAL=2 | |
B_FRAMES=3 | |
URL="rtmp://<stream url goes here>" | |
# Specify optional arguments defaults and set them if they're provided via command line. | |
TARGET_PATH=null | |
DEBUG_MODE=false | |
START_TIME="00:00:00.0" | |
EPISODE_NO=1 | |
AUDIO_TRACK=0 | |
USE_SUBTITLE_TRACK=false | |
SUBTITLE_TRACK=0 | |
FORCE_SUBTITLE_STYLE=false | |
while :; do | |
case $1 in | |
-v) TARGET_PATH="$2"; shift;; | |
-i) if [ -f $TARGET_PATH ]; then ffmpeg -i $TARGET_PATH -hide_banner; exit; else echo "Specified path was not a file."; exit; fi;; | |
-d) DEBUG_MODE=true;; | |
-t) START_TIME=$2; shift;; | |
-e) EPISODE_NO=$2; shift;; | |
-a) AUDIO_TRACK=$2; shift;; | |
-s) USE_SUBTITLE_TRACK=true; | |
if [ $2 ] && [[ "$2" != \-* ]]; then SUBTITLE_TRACK=$2; shift; fi;; | |
-f) FORCE_SUBTITLE_STYLE=true;; | |
--) shift; break;; | |
-?*) printf 'Unknown option (ignored): %s\n' "$1" >&2;; | |
*) break | |
esac | |
shift | |
done | |
# Create a playlist based on if a file or directory was provided, and starts on the provided starting episode. | |
find "$TARGET_PATH" -type f \( -iname \*.mp4 -o -iname \*.mkv \) | sort > chaika_playlist_temp.txt | |
if [[ $EPISODE_NO -gt 1 ]]; then | |
tail -n +$EPISODE_NO chaika_playlist_temp.txt > chaika_playlist.txt | |
rm chaika_playlist_temp.txt | |
else | |
mv chaika_playlist_temp.txt chaika_playlist.txt | |
fi | |
# Process each file on the playlist in sequence. | |
while read VIDEO_FILE; do | |
# Specify the subtitle video filter. | |
VIDEO_FILTER=null | |
if [ $USE_SUBTITLE_TRACK == true ]; then VIDEO_FILTER="subtitles='$VIDEO_FILE':si=$SUBTITLE_TRACK"; fi | |
if [ $FORCE_SUBTITLE_STYLE == true ]; then VIDEO_FILTER="$VIDEO_FILTER:force_style='Fontsize=24,PrimaryColour=&Hffffff&'"; fi | |
# Get the framerate and keyframerate from the video file. | |
VIDEO_FILE_NAME=`basename "$VIDEO_FILE"` | |
FRAMERATE=`ffmpeg -i "$VIDEO_FILE" 2>&1 | sed -n "s/.*, \(.*\) tbr.*/\1/p"` | |
KEYFRAMERATE=`bc <<< "$FRAMERATE * $KEYFRAME_INTERVAL"` | |
# Provide info about the stream that's about to start. | |
echo "" | |
echo "Streaming $VIDEO_FILE_NAME." | |
ffmpeg -i "$VIDEO_FILE" 2>&1 | grep "Duration" | |
if [ $START_TIME != "00:00:00.0" ]; then echo " Playing from $START_TIME."; fi | |
if [ $AUDIO_TRACK != 0 ]; then echo " Using audio track #$AUDIO_TRACK."; fi | |
if [ $USE_SUBTITLE_TRACK == true ]; then echo " Using subtitle track #$SUBTITLE_TRACK from video file."; fi | |
echo "" | |
# Start the stream. | |
if [ $DEBUG_MODE = false ]; then | |
ffmpeg \ | |
-hide_banner \ | |
-loglevel error \ | |
-stats \ | |
-nostdin \ | |
-re \ | |
-ss $START_TIME \ | |
-i "$VIDEO_FILE" \ | |
-c:v h264_nvenc \ | |
-pix_fmt yuv420p \ | |
-vf "$VIDEO_FILTER" \ | |
-preset default \ | |
-profile:v high \ | |
-threads:v 2 \ | |
-threads:a 8 \ | |
-filter_threads 2 \ | |
-f alsa \ | |
-bsf:a aac_adtstoasc \ | |
-c:a aac \ | |
-ac 2 \ | |
-b:a $AUDIO_BITRATE \ | |
-b:v $VIDEO_BITRATE \ | |
-minrate:v $VIDEO_BITRATE \ | |
-maxrate:v $VIDEO_BITRATE \ | |
-bufsize:v $VIDEO_BITRATE \ | |
-qp:v 19 \ | |
-rc:v cbr_ld_hq \ | |
-level:v auto \ | |
-r:v $FRAMERATE \ | |
-g:v $KEYFRAMERATE \ | |
-bf:v $B_FRAMES \ | |
-map 0:v \ | |
-map 0:a:$AUDIO_TRACK \ | |
-f flv $URL | |
fi | |
done < chaika_playlist.txt | |
# Delete the playlist. | |
if [ $DEBUG_MODE = false ]; then rm chaika_playlist.txt; fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment