ocTestStream.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/bin/bash
  2. # Requirements:
  3. # ffmpeg (a recent version with loop video support)
  4. # a Sans family font (for overlay text)
  5. # awk
  6. # readlink
  7. # Example: ./test/ocTestStream.sh ~/Downloads/*.mp4 rtmp://127.0.0.1/live/abc123
  8. ffmpeg_execs=( 'ffmpeg' 'ffmpeg.exe' )
  9. ffmpeg_paths=( './' '../' '' )
  10. for _ffmpeg_exec in "${ffmpeg_execs[@]}"; do
  11. for _ffmpeg_path in "${ffmpeg_paths[@]}"; do
  12. if [[ -x "$(command -v "${_ffmpeg_path}${_ffmpeg_exec}")" ]]; then
  13. ffmpeg_exec="${_ffmpeg_path}${_ffmpeg_exec}"
  14. break
  15. fi
  16. done
  17. done
  18. if [[ ${*: -1} == "--help" ]]; then
  19. echo "ocTestStream is used for sending pre-recorded or internal test content to an RTMP server."
  20. echo "Usage: ./ocTestStream.sh [VIDEO_FILES] [RTMP_DESINATION]"
  21. echo "VIDEO_FILES: path to one or multiple videos for sending to the RTMP server (optional)"
  22. echo "RTMP_DESINATION: URL of RTMP server with key (optional; default: rtmp://127.0.0.1/live/abc123)"
  23. exit
  24. elif [[ ${*: -1} == *"rtmp://"* ]]; then
  25. echo "RTMP server is specified"
  26. DESTINATION_HOST=${*: -1}
  27. FILE_COUNT=$(( ${#} - 1 ))
  28. else
  29. echo "RTMP server is not specified"
  30. DESTINATION_HOST="rtmp://127.0.0.1/live/abc123"
  31. FILE_COUNT=${#}
  32. fi
  33. if [[ -z "$ffmpeg_exec" ]]; then
  34. echo "ERROR: ffmpeg was not found in path or in the current directory! Please install ffmpeg before using this script."
  35. exit 1
  36. else
  37. ffmpeg_version=$("$ffmpeg_exec" -version | awk -F 'ffmpeg version' '{print $2}' | awk 'NR==1{print $1}')
  38. echo "ffmpeg executable: $ffmpeg_exec ($ffmpeg_version)"
  39. echo "ffmpeg path: $(readlink -f "$(which "$ffmpeg_exec")")"
  40. fi
  41. if [[ ${FILE_COUNT} -eq 0 ]]; then
  42. echo "Streaming internal test video loop to $DESTINATION_HOST"
  43. echo "...press ctrl+c to exit"
  44. command "${ffmpeg_exec}" -hide_banner -loglevel panic -nostdin -re -f lavfi \
  45. -i "testsrc=size=1280x720:rate=60[out0];sine=frequency=400:sample_rate=48000[out1]" \
  46. -vf "[in]drawtext=fontsize=96: box=1: boxcolor=black@0.75: boxborderw=5: fontcolor=white: x=(w-text_w)/2: y=((h-text_h)/2)+((h-text_h)/-2): text='Owncast Test Stream', drawtext=fontsize=96: box=1: boxcolor=black@0.75: boxborderw=5: fontcolor=white: x=(w-text_w)/2: y=((h-text_h)/2)+((h-text_h)/2): text='%{gmtime\:%H\\\\\:%M\\\\\:%S} UTC'[out]" \
  47. -nal-hrd cbr \
  48. -metadata:s:v encoder=test \
  49. -vcodec libx264 \
  50. -acodec aac \
  51. -preset veryfast \
  52. -profile:v baseline \
  53. -tune zerolatency \
  54. -bf 0 \
  55. -g 0 \
  56. -b:v 6320k \
  57. -b:a 160k \
  58. -ac 2 \
  59. -ar 48000 \
  60. -minrate 6320k \
  61. -maxrate 6320k \
  62. -bufsize 6320k \
  63. -muxrate 6320k \
  64. -r 60 \
  65. -pix_fmt yuv420p \
  66. -color_range 1 -colorspace 1 -color_primaries 1 -color_trc 1 \
  67. -flags:v +global_header \
  68. -bsf:v dump_extra \
  69. -x264-params "nal-hrd=cbr:min-keyint=2:keyint=2:scenecut=0:bframes=0" \
  70. -f flv "$DESTINATION_HOST"
  71. else
  72. CONTENT=${*:1:${FILE_COUNT}}
  73. rm -f list.txt
  74. for file in $CONTENT
  75. do
  76. if [[ -f "$file" ]]; then
  77. echo "file '$file'" >> list.txt
  78. else
  79. echo "ERROR: File not found: $file"
  80. exit 1
  81. fi
  82. done
  83. function finish {
  84. rm list.txt
  85. }
  86. trap finish EXIT
  87. echo "Streaming a loop of ${FILE_COUNT} video(s) to $DESTINATION_HOST"
  88. if [[ ${FILE_COUNT} -gt 1 ]]; then
  89. echo "Warning: If these files differ greatly in formats, transitioning from one to another may not always work correctly."
  90. fi
  91. echo "$CONTENT"
  92. echo "...press ctrl+c to exit"
  93. command "${ffmpeg_exec}" -hide_banner -loglevel panic -nostdin -stream_loop -1 -re -f concat \
  94. -safe 0 \
  95. -i list.txt \
  96. -vcodec libx264 \
  97. -profile:v high \
  98. -g 48 \
  99. -r 24 \
  100. -sc_threshold 0 \
  101. -b:v 1300k \
  102. -preset veryfast \
  103. -acodec copy \
  104. -vf drawtext="fontsize=96: box=1: boxcolor=black@0.75: boxborderw=5: fontcolor=white: x=(w-text_w)/2: y=((h-text_h)/2)+((h-text_h)/4): text='%{gmtime\:%H\\\\\:%M\\\\\:%S}'" \
  105. -f flv "$DESTINATION_HOST"
  106. fi