rewrite the stream to autodetect the adb port

This commit is contained in:
deflax 2025-04-21 21:59:16 +03:00
parent cbc68f3212
commit 93fd5a1899
5 changed files with 93 additions and 41 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
config config
rec/

View file

@ -5,10 +5,10 @@
### setup scrcpy ### setup scrcpy
`sudo apt install scrcpy` `sudo apt install scrcpy`
`sudo apt install v4l2loopback-dkms` `sudo apt install v4l2loopback-dkms v4l2loopback-utils`
### v4l2loopback.conf: ### v4l2loopback.conf:
`options v4l2loopback video_nr=7 card_label="android video"` `echo 'options v4l2loopback video_nr=5 card_label="android video"' > /etc/modprobe.d/v4l2loopback.conf`
### set wireless android camera pairing ### set wireless android camera pairing
1. enable wifi debugging 1. enable wifi debugging

View file

@ -1,11 +1,96 @@
#!/bin/bash #!/bin/bash
#
source config source config
adb connect ${android_camera}:$1 SCAN_COUNT=0
SCAN_ATTEMPTS=3
FFMPEG_LOGLEVEL="repeat+level+info"
scrcpy \ mkdir -p ./rec/
cleanup()
{
adb_pid=$(ps aux | grep 'adb' | grep ${android} | awk '{print $2}');
echo "] Killing adb services with pid: ${adb_pid}"
kill -9 ${adb_pid}
exit
}
# Trap interrupts and exit instead of continuing the loop
trap cleanup SIGINT SIGTERM
# check if the android host is even alive
if ping -c 1 ${android} &> /dev/null
then
echo "] ping success :)"
else
echo "] ping error :("
exit 1
fi
is_correct_port () {
# check if port 5555 is open
PORT=$(nmap -sT -P0 ${android} -p 5555 | awk -F/ '/tcp open/{print $1}')
if [[ $PORT == 5555 ]]; then
echo "] port 5555 is open :)"
return 0
else
return 1
fi
}
until is_correct_port || [[ $SCAN_COUNT -eq $SCAN_ATTEMPTS ]]; do
# scan and set the port to 5555
if [ ! -n "$1" ]; then
RANGE="30000-49999"
else
RANGE="$1"
fi
echo "] Checking port(s) ${RANGE}"
adb connect ${android}:$(nmap -sT -P0 ${android} -p${RANGE} | awk -F/ '/tcp open/{print $1}')
adb tcpip 5555
adb disconnect
echo -e "\n] $(( SCAN_COUNT++ ))..."
sleep 1
done
[[ $SCAN_COUNT -eq $SCAN_ATTEMPTS ]] && echo "] could not set the tcpip port :(" && (exit 1)
###
adb disconnect
adb connect ${android}:5555
scrcpy -n \
--video-source=camera --no-audio --no-video-playback \ --video-source=camera --no-audio --no-video-playback \
--camera-id=2 --camera-size=1920x1080 --camera-fps=30 --orientation=90 \ --camera-id=2 --camera-size=1920x1080 --camera-fps=30 --orientation=90 \
--v4l2-sink=/dev/video7 --v4l2-sink=/dev/video5 &
while :
do
CURRENTDATE=`date +"%Y_%m_%d_%T"`
echo "] ffmpeg start at ${CURRENTDATE}"
#calf plugins
#input_audio_filter="-af lv2=p=http\\\\://calf.sourceforge.net/plugins/Gate:c=threshold=0.04010706|knee=8|range=0.03148599, lv2=http\\\\://calf.sourceforge.net/plugins/Compressor:c=knee=8|threshold=0.125, lv2=http\\\\://calf.sourceforge.net/plugins/Limiter:c=level_out=0.71697748|limit=0.62763602"
#load normalization
#output_audio_filter="-filter:a loudnorm"
ffmpeg -hide_banner -loglevel ${FFMPEG_LOGLEVEL} -threads:v 2 -threads:a 8 -filter_threads 2 \
-thread_queue_size 512 -f v4l2 -framerate 30 -pix_fmt yuv420p -i /dev/video5 \
-thread_queue_size 512 -f alsa -ac 2 -i plughw:CARD=USB ${input_audio_filter} \
-bsf:a aac_adtstoasc -c:a aac -ac 2 -b:a 192k ${output_audio_filter} \
-init_hw_device vaapi=va:/dev/dri/renderD128 -filter_hw_device va \
-vf 'format=nv12,hwupload' \
-c:v h264_vaapi -b:v 6000k -minrate:v 6000k -maxrate:v 6000k -bufsize:v 6000k \
-r:v 30 -g:v 120 -bf:v 3 -refs:v 16 \
-f tee "[f=mpegts]${srt_output}|[f=matroska]local_recording_${CURRENTDATE}.mkv"
#CURRENTDATE=`date +"%Y_%m_%d_%T"`
echo "] ffmpeg end at ${CURRENTDATE}"
sleep 5
done

View file

@ -1,2 +1,2 @@
android_camera=192.168.0.123 android=192.168.0.123
srt_output="srt://CHANGEME:6000?mode=caller&transtype=live&pkt_size=1316&streamid=CHANGEME.stream,mode:publish,token:CHANGEME" srt_output="srt://CHANGEME:6000?mode=caller&transtype=live&pkt_size=1316&streamid=CHANGEME.stream,mode:publish,token:CHANGEME"

View file

@ -1,34 +0,0 @@
#!/bin/bash
echo "Press CTRL+C to exit"
source config
LOGLEVEL="debug"
LOGLEVEL="repeat+level+info"
while :
do
CURRENTDATE=`date +"%Y-%m-%d %T"`
echo Start at ${CURRENTDATE}
#calf plugins
#input_audio_filter="-af lv2=p=http\\\\://calf.sourceforge.net/plugins/Gate:c=threshold=0.04010706|knee=8|range=0.03148599, lv2=http\\\\://calf.sourceforge.net/plugins/Compressor:c=knee=8|threshold=0.125, lv2=http\\\\://calf.sourceforge.net/plugins/Limiter:c=level_out=0.71697748|limit=0.62763602"
#load normalization
#output_audio_filter="-filter:a loudnorm"
ffmpeg -hide_banner -loglevel ${LOGLEVEL} -threads:v 2 -threads:a 8 -filter_threads 2 \
-thread_queue_size 512 -f v4l2 -framerate 30 -pix_fmt yuv420p -i /dev/video7 \
-thread_queue_size 512 -f alsa -ac 2 -i plughw:CARD=USB ${input_audio_filter} \
-bsf:a aac_adtstoasc -c:a aac -ac 2 -b:a 192k ${output_audio_filter} \
-init_hw_device vaapi=va:/dev/dri/renderD128 -filter_hw_device va \
-vf 'format=nv12,hwupload' \
-c:v h264_vaapi -b:v 6000k -minrate:v 6000k -maxrate:v 6000k -bufsize:v 6000k \
-r:v 30 -g:v 120 -bf:v 3 -refs:v 16 \
-f mpegts ${srt_output}
CURRENTDATE=`date +"%Y-%m-%d %T"`
echo End at ${CURRENTDATE}
sleep 5
done