Comments and formatting
[streamdesktop.git] / streamdesktop
CommitLineData
902f8a5f
RR
1#!/bin/bash
2
3set -e
4
5if [ "$BASH_VERSION" = "" ];then
6 echo "This sctipt must be run with Bash, try 'bash $0'";
7 exit 1
8fi
9
10# Copyright (C) 2020 Ruben Rodriguez <ruben@gnu.org>
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26# A simple script to capture your desktop and microphone into a file and an icecast2 streaming server
27
28# Streamer side dependencies: wmctrl gstreamer1.0-tools gstreamer1.0-plugins-good gstreamer1.0-pulseaudio pulseaudio ffmpeg
29# sudo apt-get install wmctrl gstreamer1.0-tools gstreamer1.0-plugins-good gstreamer1.0-pulseaudio pulseaudio ffmpeg
30
31# Server side dependencies: icecast2
32
eeed1a54 33# Server connection settings
902f8a5f 34SERVER=my.icecast2server.org
eeed1a54
RR
35PORT=80 # port to stream to
36PASSWORD=hackme
37
38# Stream settings
39BITRATE=1000000 # Bitrate of the video, in bps
40SOUNDQUALITY=0.4 # OGG compression quality, from 0 to 1
41FRAMERATE=20 # frames per second
42SHOWPOINTER="true" # show or hide the mouse pointer
902f8a5f
RR
43# Width and height are upper limits. Video will not be upscaled or stretched, it would be proportionally downscaled to fit the WIDTH and HEIGHT dimensions
44WIDTH=1280
45HEIGHT=720
46
eeed1a54
RR
47DATE=$(date +%Y-%m-%d-%H_%M_%S)
48MOUNT=$1
49[ "$MOUNT" = "" ] && MOUNT=recording
902f8a5f
RR
50
51cat << EOF
52Usage: $0 [title]
53The title parameter will be used for the name of the mount, and the name of the file saved:
54 http://$SERVER/$MOUNT.webm
55 File saved locally to ./$MOUNT-${DATE}.webm
56 Log saved to $MOUNT-${DATE}.log
57
58AUDIO INPUT: this script will record and stream the audio from the default input selection in Pulseaudio. Click on the volume icon in your desktop environment, go to settings, then input, and select the appropriate device and input level.
59
eeed1a54 60SCREEN SELECTION. Note: broadcasting a single window may crash if you minimize or resize the screen after the streaming starts! If you have issues with this, select 0 to broadcast the full screen, but remember to close or hide any privacy-sensitive windows." If the stream is just an empty black square, make sure your desktop is running X11, not Wayland or others.
902f8a5f
RR
61
620: Capture FULL SCREEN
63EOF
64
eeed1a54
RR
65# Window selector
66WINDOWLIST="$(wmctrl -l)"
902f8a5f
RR
67i=1
68echo "$WINDOWLIST" |while read line
69do
eeed1a54
RR
70 echo -n "$i: "
71 name=$(echo $line| cut -d' ' -f 4-)
72 echo $name
73 i=$(expr $i + 1)
902f8a5f
RR
74done
75
76echo
77echo "Select the window to capture, or press enter to capture the whole screen"
78read -s -n1 SELECTION
79
80XID=$(echo "$WINDOWLIST" | sed -n "${SELECTION}p" | sed 's/ .*//')
81
82if [ "$SELECTION" = 0 ] || [ "$SELECTION" = "" ] ; then
eeed1a54 83 XPARAM=""
902f8a5f 84else
eeed1a54 85 XPARAM="xid=$XID"
902f8a5f
RR
86fi
87
eeed1a54
RR
88
89# The video needs to be processed after recording
902f8a5f
RR
90trap ctrl_c INT
91ctrl_c() {
92 echo
93 echo -n "Remuxing streamed video... "
94 mv $MOUNT-${DATE}.webm $MOUNT-${DATE}.webm.bak
95 ffmpeg -i $MOUNT-${DATE}.webm.bak -acodec copy -vcodec copy $MOUNT-${DATE}.webm >> $MOUNT-${DATE}.log 2>&1
96 echo done.
97 echo
98 echo Use $MOUNT-${DATE}.webm for publishing, you can delete $MOUNT-${DATE}.webm.bak
99 echo
100 echo "You can trim the beginning and end of the video with this command (replace the timestamps, HH:MM:SS):"
101 echo ffmpeg -i $MOUNT-${DATE}.webm -acodec copy -vcodec copy -ss 00:19:27 -to 02:18:51 trimmed.webm
102}
103
eeed1a54 104
902f8a5f
RR
105echo STARTING STREAM in 2 seconds. Press CTRL+C to end the session.
106sleep 2
107echo Streaming at http://$SERVER/recording.webm
108echo
109
eeed1a54 110# GSTREAMER 1.0 pipeline for capture, save to file, and stream to icecast2
902f8a5f
RR
111gst-launch-1.0 --eos-on-shutdown ximagesrc use-damage=false $XPARAM show-pointer=$SHOWPOINTER \
112! videorate ! video/x-raw,framerate=$FRAMERATE/1 \
113! videoscale ! video/x-raw, width=$WIDTH, height=$HEIGHT \
114! videoconvert ! queue ! vp8enc target_bitrate=$BITRATE cpu-used=0 deadline=1 threads=4 keyframe-max-dist=15 ! queue ! mux. \
115pulsesrc ! vorbisenc quality=$SOUNDQUALITY ! queue ! mux. \
116webmmux streamable=true name=mux ! tee name=stream ! queue ! filesink location=$MOUNT-${DATE}.webm sync=false \
117stream. ! queue max-size-time=0 max-size-bytes=135528448 max-size-buffers=0 ! shout2send ip=$SERVER port=$PORT mount=$MOUNT.webm password=$PASSWORD sync=false \
1182>&1 | tee $MOUNT-${DATE}.log
119
120