Skip to content

Instantly share code, notes, and snippets.

@zwh8800
Last active February 22, 2020 11:11
Show Gist options
  • Save zwh8800/43887bd1d8af2d0d9684f90dc48e12c0 to your computer and use it in GitHub Desktop.
Save zwh8800/43887bd1d8af2d0d9684f90dc48e12c0 to your computer and use it in GitHub Desktop.
#!/bin/bash
main_list_file="$1"
base_dir="$2"
lock_file="$base_dir/live.lock"
cd $base_dir
dir=''
function onExit() {
rm -f "$dir/streaming"
}
trap onExit EXIT
exec 320<>$lock_file
while :
do
dir=`shuf $main_list_file | head -1`
echo 'dir: ' "$dir"
if [ -z "$dir" ]; then
continue
fi
flock -w 3 320
if [ ! -f "$dir/streaming" ]; then
touch "$dir/streaming"
else
continue
fi
flock -u 320
line=`find "$dir" | grep -E 'mkv|mp4|flv' | sort | wc -l`
start=`expr $RANDOM % $line`
end=`expr $line - $start`
list_file="list$3"
echo '' > $list_file
for file in `find "$dir" | grep -E 'mkv|mp4|flv' | sort | tail -n $end`; do
probe_file=$file
echo 'file' $file >> $list_file
done;
for file in `find "$dir" | grep -E 'mkv|mp4|flv' | sort | head -n $start`; do
echo 'file' $file >> $list_file
done;
if ffprobe "$probe_file" 2>&1 | grep -E 'eac3|dts'; then
load=`cat /proc/loadavg | awk '{print $3}'`
if [ `echo "$load > 2" | bc` -eq 1 ]; then
rm -f "$dir/streaming"
continue
fi
ffmpeg -loglevel warning -y -re \
-f concat -safe 0 -i $list_file \
-map 0 -c:v copy -c:a ac3 -sn \
-f hls -hls_list_size 6 -hls_wrap 10 -hls_time 5 -hls_segment_filename "live$3-%03d.ts" $base_dir/live$3.m3u8 \
-map 0 -c:v copy -c:a ac3 -sn \
-f hls -hls_segment_type fmp4 -hls_fmp4_init_filename ios-init$3.mp4 -hls_list_size 6 -hls_wrap 10 -hls_time 5 -hls_segment_filename "ios-live$3-%03d.ts" $base_dir/ios-live$3.m3u8
else
ffmpeg -loglevel warning -y -re \
-f concat -safe 0 -i $list_file \
-map 0 -c copy -sn \
-f hls -hls_list_size 6 -hls_wrap 10 -hls_time 5 -hls_segment_filename "live$3-%03d.ts" $base_dir/live$3.m3u8 \
-map 0 -c copy -sn \
-f hls -hls_segment_type fmp4 -hls_fmp4_init_filename ios-init$3.mp4 -hls_list_size 6 -hls_wrap 10 -hls_time 5 -hls_segment_filename "ios-live$3-%03d.ts" $base_dir/ios-live$3.m3u8
fi
rm -f "$dir/streaming"
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment