-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathowncast_stream_recorder.sh
108 lines (94 loc) · 3.22 KB
/
owncast_stream_recorder.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/bin/bash
# Configuration variables
API_URL="https://<owncast-server>/api/status"
M3U8_STREAM_URL="https://<owncast-server>/hls/0/stream.m3u8"
RECORDINGS_DIR="<path-to-store-recordings>"
FILE_NAME_PREFIX="stream_"
# Function to check if stream is online
is_stream_online() {
response=$(curl -s "$API_URL")
echo "$response" | grep -q '"online":true'
return $?
}
# Function to start recording
start_recording() {
timestamp=$(date +"%Y%m%d_%H%M%S")
output_file="${RECORDINGS_DIR}/${FILE_NAME_PREFIX}${timestamp}.mp4"
ffmpeg -analyzeduration 1000000 -probesize 1500000 -i "$M3U8_STREAM_URL" -c copy "$output_file" &
echo $! > /tmp/ffmpeg_pid
}
# Function to stop recording
stop_recording() {
if [ -f /tmp/ffmpeg_pid ]; then
pid=$(cat /tmp/ffmpeg_pid)
if kill -0 $pid 2>/dev/null; then
kill $pid
rm /tmp/ffmpeg_pid
else
echo "Recording process $pid not found. It may have already ended."
rm /tmp/ffmpeg_pid
fi
fi
manage_recordings
sleep 1
remux_videos
}
# Function to manage recordings
manage_recordings() {
cd "$RECORDINGS_DIR" || { echo "Directory not found: $RECORDINGS_DIR"; return 1; }
echo "Looking for files in $RECORDINGS_DIR"
recordings=($(ls -t *.mp4))
echo "Found ${#recordings[@]} recordings."
if [ ${#recordings[@]} -gt 6 ]; then
for ((i=6; i<${#recordings[@]}; i++)); do
echo "Deleting: ${recordings[$i]}"
rm "${recordings[$i]}"
done
fi
}
remux_videos() {
cd "$RECORDINGS_DIR" || { echo "Directory not found: $RECORDINGS_DIR"; return 1; }
echo "Looking for files in $RECORDINGS_DIR"
# Loop through all mp4 files sorted by modification time
for file in *.mp4; do
if [ -f "$file" ]; then
echo "Processing $file..."
# Define the output file name for the re-muxed video
temp_file="${file%.mp4}_temp.mp4"
# Re-mux the file to ensure metadata is at the beginning
ffmpeg -i "$file" -c copy -movflags +faststart "$temp_file"
# Check if the re-muxing was successful
if [ $? -eq 0 ]; then
echo "Successfully re-muxed $file to $temp_file"
# Remove the original file and rename the re-muxed file
rm "$file"
mv "$temp_file" "$file"
else
echo "Failed to re-mux $file"
# Remove the temporary file if re-muxing failed
rm "$temp_file"
fi
fi
done
}
# Main loop
while true; do
if is_stream_online; then
if [ ! -f /tmp/ffmpeg_pid ]; then
echo "Stream is online. Waiting 30 seconds before starting recording..."
sleep 30
if is_stream_online; then # Check again to ensure the stream is still online after waiting
echo "Starting recording..."
start_recording
else
echo "Stream went offline during the wait period. Not starting recording."
fi
fi
else
if [ -f /tmp/ffmpeg_pid ]; then
echo "Stream is offline. Stopping recording..."
stop_recording
fi
fi
sleep 30
done