2022-11-26 23:41:41 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
#
|
2024-07-06 18:43:37 +00:00
|
|
|
set -euo pipefail
|
|
|
|
IFS=$'\n\t'
|
|
|
|
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
2022-11-26 23:41:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2024-07-06 18:43:37 +00:00
|
|
|
function main {
|
|
|
|
local cmd
|
|
|
|
cmd=$1
|
|
|
|
shift
|
|
|
|
if [ "$cmd" = "copy" ]; then
|
|
|
|
copy "${@}"
|
2024-10-10 22:12:39 +00:00
|
|
|
elif [ "$cmd" = "av1" ]; then
|
|
|
|
av1 "${@}"
|
2024-10-10 01:09:11 +00:00
|
|
|
elif [ "$cmd" = "stream_software_h264" ]; then
|
|
|
|
stream_software_h264 "${@}"
|
|
|
|
elif [ "$cmd" = "stream_hardware_h264" ]; then
|
|
|
|
stream_hardware_h264 "${@}"
|
|
|
|
elif [ "$cmd" = "preprocess_software_h264" ]; then
|
|
|
|
preprocess_software_h264 "${@}"
|
2024-07-19 03:29:07 +00:00
|
|
|
elif [ "$cmd" = "preprocess_hardware_h264" ]; then
|
|
|
|
preprocess_hardware_h264 "${@}"
|
|
|
|
elif [ "$cmd" = "vp9" ]; then
|
|
|
|
vp9 "${@}"
|
|
|
|
elif [ "$cmd" = "preprocess_hardware_vp9" ]; then
|
|
|
|
preprocess_hardware_vp9 "${@}"
|
|
|
|
elif [ "$cmd" = "vp8" ]; then
|
|
|
|
vp8 "${@}"
|
2024-07-13 17:15:04 +00:00
|
|
|
elif [ "$cmd" = "software_vp8" ]; then
|
|
|
|
software_vp8 "${@}"
|
2024-07-06 18:43:37 +00:00
|
|
|
elif [ "$cmd" = "preprocess_h264" ]; then
|
|
|
|
preprocess_h264 "${@}"
|
|
|
|
elif [ "$cmd" = "preprocess_vp8" ]; then
|
|
|
|
preprocess_vp8 "${@}"
|
|
|
|
elif [ "$cmd" = "webcam" ]; then
|
|
|
|
webcam "${@}"
|
|
|
|
elif [ "$cmd" = "encode_webcam" ]; then
|
|
|
|
encode_webcam "${@}"
|
|
|
|
else
|
|
|
|
(>&2 echo "Unknown command: $cmd")
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function copy {
|
|
|
|
local file_to_cast
|
|
|
|
file_to_cast="$3"
|
|
|
|
|
|
|
|
local USERNAME PASSWORD
|
|
|
|
USERNAME="$1"
|
|
|
|
PASSWORD="$2"
|
|
|
|
|
2024-07-19 03:29:07 +00:00
|
|
|
set -x
|
2024-07-22 01:48:48 +00:00
|
|
|
</dev/null exec ffmpeg \
|
2024-07-06 18:43:37 +00:00
|
|
|
-re \
|
|
|
|
-stream_loop -1 \
|
|
|
|
-i "$file_to_cast" \
|
|
|
|
-c copy \
|
2024-07-19 03:29:07 +00:00
|
|
|
-strict experimental \
|
2024-07-06 18:43:37 +00:00
|
|
|
-f rtsp \
|
2024-07-17 23:46:43 +00:00
|
|
|
-rtsp_transport tcp \
|
2024-07-06 18:43:37 +00:00
|
|
|
"rtsp://$USERNAME:$PASSWORD@172.16.16.251:8554/fetch"
|
|
|
|
}
|
|
|
|
|
2024-10-10 22:12:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
function av1 {
|
|
|
|
# local additional_flags=()
|
|
|
|
# additional_flags+=(--profile "$PROFILE")
|
|
|
|
# (cd "$DIR/../" && cargo build --no-default-features "${additional_flags[@]}")
|
|
|
|
|
|
|
|
local destination_type="$1" # "stream" or "preprocess"
|
|
|
|
local acceleration_type="$2" # "software" or "hardware"
|
|
|
|
# shift 2
|
|
|
|
|
|
|
|
local args=()
|
|
|
|
|
|
|
|
if [ "$destination_type" == "stream" ]; then
|
|
|
|
args+=(-re -stream_loop -1)
|
|
|
|
elif [ "$destination_type" == "preproces" ]; then
|
|
|
|
true
|
|
|
|
else
|
|
|
|
(>&2 echo "Unknown destination type: $destination_type")
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$acceleration_type" == "software" ]; then
|
|
|
|
true
|
|
|
|
elif [ "$acceleration_type" == "hardware" ]; then
|
|
|
|
args+=(-vaapi_device /dev/dri/renderD128)
|
|
|
|
else
|
|
|
|
(>&2 echo "Unknown acceleration type: $acceleration_type")
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
args+=(-i "$file_to_cast")
|
|
|
|
|
|
|
|
if [ "$acceleration_type" == "software" ]; then
|
|
|
|
args+=(-c:v h264)
|
|
|
|
elif [ "$acceleration_type" == "hardware" ]; then
|
|
|
|
args+=(-vf 'format=nv12|vaapi,hwupload')
|
|
|
|
args+=(-c:v h264_vaapi)
|
|
|
|
else
|
|
|
|
(>&2 echo "Unknown acceleration type: $acceleration_type")
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
args+=(-b:v 2M)
|
|
|
|
args+=(-profile:v high)
|
|
|
|
args+=(-bf 0)
|
|
|
|
args+=(-strict -2)
|
|
|
|
args+=(-c:a opus)
|
|
|
|
args+=(-ac 2)
|
|
|
|
args+=(-b:a 320k)
|
|
|
|
args+=(-ar 48000)
|
|
|
|
|
|
|
|
if [ "$destination_type" == "stream" ]; then
|
|
|
|
args+=(-f rtsp)
|
|
|
|
args+=(-rtsp_transport tcp)
|
|
|
|
args+=("rtsp://$USERNAME:$PASSWORD@172.16.16.251:8554/fetch")
|
|
|
|
elif [ "$destination_type" == "preproces" ]; then
|
|
|
|
args+=("$file_to_save")
|
|
|
|
else
|
|
|
|
(>&2 echo "Unknown destination type: $destination_type")
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-10-10 01:09:11 +00:00
|
|
|
function stream_software_h264 {
|
2024-07-06 18:43:37 +00:00
|
|
|
local file_to_cast
|
|
|
|
file_to_cast="$3"
|
|
|
|
|
|
|
|
local USERNAME PASSWORD
|
|
|
|
USERNAME="$1"
|
|
|
|
PASSWORD="$2"
|
|
|
|
|
2024-07-19 03:29:07 +00:00
|
|
|
set -x
|
|
|
|
|
2024-07-06 18:43:37 +00:00
|
|
|
# -bf 0 :: Disable b-frames because webrtc doesn't support h264 streams with b-frames.
|
2024-07-22 01:48:48 +00:00
|
|
|
</dev/null exec ffmpeg \
|
2024-10-10 01:09:11 +00:00
|
|
|
-re \
|
|
|
|
-stream_loop -1 \
|
|
|
|
-i "$file_to_cast" \
|
|
|
|
-c:v h264 \
|
|
|
|
-b:v 2M \
|
|
|
|
-profile:v high \
|
|
|
|
-bf 0 \
|
|
|
|
-strict -2 \
|
|
|
|
-c:a opus \
|
|
|
|
-ac 2 \
|
|
|
|
-b:a 320k \
|
|
|
|
-ar 48000 \
|
|
|
|
-f rtsp \
|
|
|
|
-rtsp_transport tcp \
|
|
|
|
"rtsp://$USERNAME:$PASSWORD@172.16.16.251:8554/fetch"
|
2024-07-06 18:43:37 +00:00
|
|
|
}
|
|
|
|
|
2024-10-10 01:09:11 +00:00
|
|
|
function stream_hardware_h264 {
|
|
|
|
local file_to_cast
|
|
|
|
file_to_cast="$3"
|
|
|
|
|
|
|
|
local USERNAME PASSWORD
|
|
|
|
USERNAME="$1"
|
|
|
|
PASSWORD="$2"
|
2024-07-19 03:29:07 +00:00
|
|
|
|
|
|
|
set -x
|
|
|
|
|
|
|
|
# -bf 0 :: Disable b-frames because webrtc doesn't support h264 streams with b-frames.
|
2024-07-22 01:48:48 +00:00
|
|
|
</dev/null exec ffmpeg \
|
2024-10-10 01:09:11 +00:00
|
|
|
-re \
|
|
|
|
-stream_loop -1 \
|
|
|
|
-vaapi_device /dev/dri/renderD128 \
|
|
|
|
-i "$file_to_cast" \
|
|
|
|
-vf 'format=nv12|vaapi,hwupload' \
|
|
|
|
-c:v h264_vaapi \
|
|
|
|
-b:v 2M \
|
|
|
|
-profile:v high \
|
|
|
|
-bf 0 \
|
|
|
|
-strict -2 \
|
|
|
|
-c:a opus \
|
|
|
|
-ac 2 \
|
|
|
|
-b:a 320k \
|
|
|
|
-ar 48000 \
|
|
|
|
-f rtsp \
|
|
|
|
-rtsp_transport tcp \
|
|
|
|
"rtsp://$USERNAME:$PASSWORD@172.16.16.251:8554/fetch"
|
2024-07-19 03:29:07 +00:00
|
|
|
}
|
|
|
|
|
2024-10-10 01:09:11 +00:00
|
|
|
function preprocess_software_h264 {
|
|
|
|
local file_to_cast file_to_save
|
|
|
|
file_to_cast="$1"
|
|
|
|
file_to_save="$2"
|
2024-07-06 18:43:37 +00:00
|
|
|
|
2024-07-19 03:29:07 +00:00
|
|
|
set -x
|
|
|
|
|
2024-07-06 18:43:37 +00:00
|
|
|
# -bf 0 :: Disable b-frames because webrtc doesn't support h264 streams with b-frames.
|
2024-07-22 01:48:48 +00:00
|
|
|
</dev/null exec ffmpeg \
|
2024-10-10 01:09:11 +00:00
|
|
|
-i "$file_to_cast" \
|
|
|
|
-c:v h264 \
|
|
|
|
-b:v 2M \
|
|
|
|
-profile:v high \
|
|
|
|
-bf 0 \
|
|
|
|
-strict -2 \
|
|
|
|
-c:a opus \
|
|
|
|
-ac 2 \
|
|
|
|
-b:a 320k \
|
|
|
|
-ar 48000 \
|
|
|
|
"$file_to_save"
|
2024-07-06 18:43:37 +00:00
|
|
|
}
|
|
|
|
|
2024-10-10 01:09:11 +00:00
|
|
|
function preprocess_hardware_h264 {
|
2024-07-06 18:43:37 +00:00
|
|
|
local file_to_cast file_to_save
|
|
|
|
file_to_cast="$1"
|
|
|
|
file_to_save="$2"
|
|
|
|
|
2024-07-19 03:29:07 +00:00
|
|
|
set -x
|
|
|
|
|
2024-07-06 18:43:37 +00:00
|
|
|
# -bf 0 :: Disable b-frames because webrtc doesn't support h264 streams with b-frames.
|
2024-07-22 01:48:48 +00:00
|
|
|
</dev/null exec ffmpeg \
|
2024-10-10 01:09:11 +00:00
|
|
|
-vaapi_device /dev/dri/renderD128 \
|
|
|
|
-i "$file_to_cast" \
|
|
|
|
-vf 'format=nv12,hwupload' \
|
|
|
|
-c:v h264_vaapi \
|
|
|
|
-b:v 2M \
|
|
|
|
-profile:v high \
|
|
|
|
-bf 0 \
|
|
|
|
-strict -2 \
|
|
|
|
-c:a opus \
|
|
|
|
-ac 2 \
|
|
|
|
-b:a 320k \
|
|
|
|
-ar 48000 \
|
|
|
|
"$file_to_save"
|
2024-07-06 18:43:37 +00:00
|
|
|
}
|
|
|
|
|
2024-07-19 03:29:07 +00:00
|
|
|
function vp9 {
|
|
|
|
local file_to_cast
|
|
|
|
file_to_cast="$3"
|
|
|
|
|
|
|
|
local USERNAME PASSWORD
|
|
|
|
USERNAME="$1"
|
|
|
|
PASSWORD="$2"
|
|
|
|
|
|
|
|
set -x
|
|
|
|
|
|
|
|
# -bf 0 :: Disable b-frames because webrtc doesn't support h264 streams with b-frames.
|
2024-07-22 01:48:48 +00:00
|
|
|
</dev/null exec ffmpeg \
|
2024-07-19 03:29:07 +00:00
|
|
|
-re \
|
|
|
|
-stream_loop -1 \
|
|
|
|
-init_hw_device vaapi=foo:/dev/dri/renderD128 \
|
|
|
|
-hwaccel vaapi \
|
|
|
|
-hwaccel_output_format vaapi \
|
|
|
|
-hwaccel_device foo \
|
|
|
|
-i "$file_to_cast" \
|
|
|
|
-filter_hw_device foo \
|
|
|
|
-vf 'format=nv12|vaapi,hwupload' \
|
|
|
|
-c:v vp9_vaapi \
|
|
|
|
-bf 0 \
|
|
|
|
-strict -2 \
|
|
|
|
-c:a opus \
|
|
|
|
-b:a 320k \
|
|
|
|
-ar 48000 \
|
|
|
|
-f rtsp \
|
|
|
|
-rtsp_transport tcp \
|
|
|
|
"rtsp://$USERNAME:$PASSWORD@172.16.16.251:8554/fetch"
|
|
|
|
}
|
|
|
|
|
|
|
|
function preprocess_hardware_vp9 {
|
2024-07-06 18:43:37 +00:00
|
|
|
local file_to_cast file_to_save
|
|
|
|
file_to_cast="$1"
|
|
|
|
file_to_save="$2"
|
|
|
|
|
2024-07-19 03:29:07 +00:00
|
|
|
set -x
|
|
|
|
|
2024-07-06 18:43:37 +00:00
|
|
|
# -bf 0 :: Disable b-frames because webrtc doesn't support h264 streams with b-frames.
|
2024-07-22 01:48:48 +00:00
|
|
|
</dev/null exec ffmpeg \
|
2024-07-19 03:29:07 +00:00
|
|
|
-init_hw_device vaapi=foo:/dev/dri/renderD128 \
|
|
|
|
-hwaccel vaapi \
|
|
|
|
-hwaccel_output_format vaapi \
|
|
|
|
-hwaccel_device foo \
|
2024-07-06 18:43:37 +00:00
|
|
|
-i "$file_to_cast" \
|
2024-07-19 03:29:07 +00:00
|
|
|
-filter_hw_device foo \
|
|
|
|
-vf 'format=nv12|vaapi,hwupload' \
|
|
|
|
-c:v vp9_vaapi \
|
2024-07-06 18:43:37 +00:00
|
|
|
-bf 0 \
|
2024-07-19 03:29:07 +00:00
|
|
|
-strict -2 \
|
2024-07-06 18:43:37 +00:00
|
|
|
-c:a opus \
|
|
|
|
-b:a 320k \
|
|
|
|
-ar 48000 \
|
|
|
|
"$file_to_save"
|
|
|
|
}
|
|
|
|
|
2024-07-13 17:15:04 +00:00
|
|
|
function software_vp8 {
|
|
|
|
local USERNAME PASSWORD
|
|
|
|
USERNAME="$1"
|
|
|
|
PASSWORD="$2"
|
|
|
|
|
|
|
|
local file_to_cast
|
|
|
|
file_to_cast="$3"
|
|
|
|
|
2024-07-19 03:29:07 +00:00
|
|
|
set -x
|
|
|
|
|
2024-07-13 17:15:04 +00:00
|
|
|
# -bf 0 :: Disable b-frames because webrtc doesn't support h264 streams with b-frames.
|
|
|
|
# -strict -2 :: Enable support for experimental codecs like opus.
|
2024-07-22 01:48:48 +00:00
|
|
|
# -b:v 2M :: Target 2 megabit/s
|
2024-07-13 17:15:04 +00:00
|
|
|
# -crf 10 :: Target a quality level and adjust bitrate accordingly. This should be preferred, but ideally both should be used.
|
2024-07-22 01:48:48 +00:00
|
|
|
</dev/null exec ffmpeg \
|
2024-07-13 17:15:04 +00:00
|
|
|
-re \
|
|
|
|
-stream_loop -1 \
|
|
|
|
-i "$file_to_cast" \
|
|
|
|
-c:v vp8 \
|
2024-07-22 01:48:48 +00:00
|
|
|
-b:v 2M \
|
2024-07-13 17:15:04 +00:00
|
|
|
-crf 10 \
|
|
|
|
-bf 0 \
|
|
|
|
-c:a opus \
|
|
|
|
-b:a 320k \
|
|
|
|
-ar 48000 \
|
|
|
|
-strict -2 \
|
|
|
|
-f rtsp \
|
2024-07-17 23:46:43 +00:00
|
|
|
-rtsp_transport tcp \
|
2024-07-13 17:15:04 +00:00
|
|
|
"rtsp://$USERNAME:$PASSWORD@172.16.16.251:8554/fetch"
|
|
|
|
}
|
|
|
|
|
2024-07-19 03:29:07 +00:00
|
|
|
function preprocess_vp8 {
|
|
|
|
local file_to_cast file_to_save
|
|
|
|
file_to_cast="$1"
|
|
|
|
file_to_save="$2"
|
|
|
|
|
|
|
|
set -x
|
|
|
|
|
|
|
|
# -bf 0 :: Disable b-frames because webrtc doesn't support h264 streams with b-frames.
|
|
|
|
# -strict -2 :: Enable support for experimental codecs like opus.
|
2024-07-22 01:48:48 +00:00
|
|
|
# -b:v 2M :: Target 2 megabit/s
|
2024-07-19 03:29:07 +00:00
|
|
|
# -crf 10 :: Target a quality level and adjust bitrate accordingly. This should be preferred, but ideally both should be used.
|
2024-07-22 01:48:48 +00:00
|
|
|
</dev/null exec ffmpeg \
|
2024-07-19 03:29:07 +00:00
|
|
|
-i "$file_to_cast" \
|
|
|
|
-c:v vp8 \
|
2024-07-22 01:48:48 +00:00
|
|
|
-b:v 2M \
|
2024-07-19 03:29:07 +00:00
|
|
|
-crf 10 \
|
|
|
|
-bf 0 \
|
|
|
|
-c:a opus \
|
|
|
|
-b:a 320k \
|
|
|
|
-ar 48000 \
|
|
|
|
-strict -2 \
|
|
|
|
"$file_to_save"
|
|
|
|
}
|
|
|
|
|
2024-07-06 18:43:37 +00:00
|
|
|
function webcam {
|
|
|
|
# Uses on-webcam h264 encoding.
|
|
|
|
|
|
|
|
local USERNAME PASSWORD
|
|
|
|
USERNAME="$1"
|
|
|
|
PASSWORD="$2"
|
|
|
|
|
2024-07-19 03:29:07 +00:00
|
|
|
set -x
|
|
|
|
|
2024-07-22 01:48:48 +00:00
|
|
|
</dev/null exec ffmpeg \
|
2024-07-06 18:43:37 +00:00
|
|
|
-re \
|
|
|
|
-input_format h264 \
|
|
|
|
-video_size 1920x1080 \
|
|
|
|
-i /dev/video0 \
|
|
|
|
-c:v copy \
|
|
|
|
-an \
|
|
|
|
-f rtsp \
|
2024-07-17 23:46:43 +00:00
|
|
|
-rtsp_transport tcp \
|
2024-07-06 18:43:37 +00:00
|
|
|
"rtsp://$USERNAME:$PASSWORD@172.16.16.251:8554/fetch"
|
|
|
|
}
|
|
|
|
|
|
|
|
function encode_webcam {
|
|
|
|
# Uses hardware accelerated gpu-based encoding.
|
|
|
|
|
|
|
|
local USERNAME PASSWORD
|
|
|
|
USERNAME="$1"
|
|
|
|
PASSWORD="$2"
|
|
|
|
|
2024-07-19 03:29:07 +00:00
|
|
|
set -x
|
|
|
|
|
2024-07-22 01:48:48 +00:00
|
|
|
</dev/null exec ffmpeg \
|
2024-07-06 18:43:37 +00:00
|
|
|
-re \
|
|
|
|
-vaapi_device /dev/dri/renderD128 \
|
|
|
|
-i /dev/video0 \
|
|
|
|
-vf 'format=nv12,hwupload' \
|
|
|
|
-c:v h264_vaapi \
|
|
|
|
-an \
|
|
|
|
-f rtsp \
|
2024-07-17 23:46:43 +00:00
|
|
|
-rtsp_transport tcp \
|
2024-07-06 18:43:37 +00:00
|
|
|
"rtsp://$USERNAME:$PASSWORD@172.16.16.251:8554/fetch"
|
|
|
|
}
|
|
|
|
|
2024-09-03 23:32:42 +00:00
|
|
|
function speed_up_preprocess_vp8 {
|
|
|
|
local file_to_cast file_to_save
|
|
|
|
file_to_cast="$1"
|
|
|
|
file_to_save="$2"
|
|
|
|
|
|
|
|
set -x
|
|
|
|
|
|
|
|
# -bf 0 :: Disable b-frames because webrtc doesn't support h264 streams with b-frames.
|
|
|
|
# -strict -2 :: Enable support for experimental codecs like opus.
|
|
|
|
# -b:v 2M :: Target 2 megabit/s
|
|
|
|
# -crf 10 :: Target a quality level and adjust bitrate accordingly. This should be preferred, but ideally both should be used.
|
|
|
|
# Could also use -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" -map "[v]" -map "[a]"
|
|
|
|
</dev/null exec ffmpeg \
|
|
|
|
-i "$file_to_cast" \
|
|
|
|
-filter:v "setpts=0.66666666*PTS" \
|
|
|
|
-filter:a "atempo=1.5" \
|
|
|
|
-c:v vp8 \
|
|
|
|
-b:v 2M \
|
|
|
|
-crf 10 \
|
|
|
|
-bf 0 \
|
|
|
|
-c:a opus \
|
|
|
|
-b:a 320k \
|
|
|
|
-ar 48000 \
|
|
|
|
-strict -2 \
|
|
|
|
"$file_to_save"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-06 18:43:37 +00:00
|
|
|
main "${@}"
|