Skip to content
Snippets Groups Projects
Commit 741a26d7 authored by flisk's avatar flisk
Browse files

iterating

parent fbad2db9
Branches
No related merge requests found
url
config
# vim: ft=sh
# Copy this file to 'config' and adjust the variables as needed.
#
# NOTE: Your streaming credentials will be in this file, so you should make
# sure unauthorized users don't have access to it:
#
# $ umask 077
# $ cp config.sample config
#
# How much bandwidth you want the stream to consume. If it's not plentiful, run
# a speed test and subtract ~100 kB/s from your upload result.
upload_bandwidth_kibs=400
# This should be fine as-is.
audio_bitrate=192
# Your streaming credentials go into this URL.
# If you don't yet have streaming credentials, ask on IRC.
url="rtmp://lainchan.org/show/stream?user=changeme&pass=changeme"
#!/usr/bin/env bash
scriptname="$(basename "$0")"
tmp_file=tmp/stream.flv
fail() {
>&2 echo "$scriptname: $*"
exit 1
}
fail_usage() {
>&2 echo "$scriptname: $*"
>&2 usage
exit 1
}
usage() {
cat <<EOF
usage: $scriptname transcode|stream ...
$scriptname transcode <video file> [<subtitles file>]
Prepares a stream by transcoding the given video file into a
streamable format. The subtitles argument is optional, but
please consider Lainons with impaired hearing.
The transcoded file will be '$tmp_file'. After you're done
streaming, you will have to delete this file manually for now.
Technically the script could auto-delete it after the 'stream'
command finishes, but this would go wrong when transcoding
speed falls below 1x, and transcoding is expensive.
$scriptname stream
Transmit the transcoded file to the configured server.
You should be able to start streaming while the transcode is
still running. Use some common sense in deciding when/if it's
appropriate to start a stream based on the speed reported by
ffmpeg (1x or higher should be fine).
EOF
}
load_config() {
. config
upload_bandwidth_kbits=$(( upload_bandwidth_kibs * 8 ))
video_bitrate=$(( $upload_bandwidth_kbits - $audio_bitrate ))
buffer_size=$(( $video_bitrate + $audio_bitrate ))
}
transcode() {
if [[ $# < 1 || $# > 2 ]]; then
fail_usage "invalid number of arguments"
fi
if [[ -e "$tmp_file" ]]; then
fail "$tmp_file exists; is a transcode running already?"
fi
load_config
filter_complex="scale=-1:720"
if [[ $# > 1 ]]; then
filter_complex="subtitles=$(printf %q "$2"):force_style='Fontname=JMH Typewriter':fontsdir=fonts,$filter_complex"
fi
opts=(
-hide_banner
-threads $(nproc)
-i "$1"
# audio
-b:a ${audio_bitrate}k
-c:a aac
-ac 2
# video
-b:v ${video_bitrate}k
-c:v h264
-preset faster
-tune zerolatency
-filter_complex "$filter_complex"
# streaming
-maxrate ${buffer_size}k
-bufsize ${buffer_size}k
-f flv
"$tmp_file"
)
ffmpeg "${opts[@]}"
}
stream() {
if [[ $# != 0 ]]; then
fail "too many arguments"
fi
load_config
opts=(
-hide_banner
-re
-i "$tmp_file"
-c copy
-maxrate "${buffer_size}k"
-bufsize "${buffer_size}k"
-f flv
"$url"
)
ffmpeg "${opts[@]}"
}
if [[ $# == 0 ]]; then
fail_usage "missing command"
fi
command="$1"
shift
case "$command" in
transcode) transcode "$@" ;;
stream) stream "$@" ;;
*) fail_usage "unknown command: $command"
esac
#!/usr/bin/env bash
if [[ $# < 1 || $# > 3 ]]; then
>&2 echo "usage: $(basename "$0") <video file> [<subtitles file>] [<timestamp>]"
exit 1
fi
# How much bandwidth you want the stream to consume. If it's not plentiful, run
# a speed test and subtract 100 kB/s from your upload result.
upload_bandwidth_kibs=350
upload_bandwidth_kbits=$(( upload_bandwidth_kibs * 8 ))
audio_bitrate=192
video_bitrate=$(( $upload_bandwidth_kbits - $audio_bitrate ))
buffer_size=$(( $video_bitrate + $audio_bitrate ))
filter_complex="scale=-1:720"
if [[ $# > 1 ]]; then
filter_complex="subtitles=$(printf %q "$2"):force_style='Fontname=JMH Typewriter':fontsdir=fonts,$filter_complex"
fi
input_opts=()
if [[ $# > 2 ]]; then
input_opts+=(-ss "$3")
fi
opts=(
-threads $(nproc)
-re
"${input_opts[@]}"
-i "$1"
# audio
-b:a ${audio_bitrate}k
-c:a aac
-ac 2
# video
-b:v ${video_bitrate}k
-c:v h264
-preset faster
-tune zerolatency
-filter_complex "$filter_complex"
# streaming
-maxrate ${buffer_size}k
-bufsize ${buffer_size}k
-f flv
"$(cat url)"
)
echo "audio bitrate : $audio_bitrate"
echo "video bitrate : $video_bitrate"
echo "buffer size : $buffer_size"
echo "ffmpeg options : ${opts[@]}"
ffmpeg "${opts[@]}"
*
!.gitignore
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment