Guest User

Download audio of Mogra Twitch & Ustream streams

a guest
Sep 29th, 2018
173
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # The following should work on Debian Stretch, Ubuntu 18.04 and maybe Ubuntu 16.04
  2.  
  3. sudo apt-get update
  4. sudo apt-get install python3 python3-venv ffmpeg
  5. sudo useradd --home-dir /var/lib/mogra-dl --create-home --system --shell /sbin/nologin --user-group mogra-dl
  6.  
  7. sudo -u mogra-dl bash
  8.  
  9. python3 -m venv /var/lib/mogra-dl/.local/python-env
  10. /var/lib/mogra-dl/.local/python-env/bin/pip3 install --upgrade --upgrade-strategy only-if-needed pip
  11. /var/lib/mogra-dl/.local/python-env/bin/pip3 install --upgrade --upgrade-strategy only-if-needed setuptools
  12. /var/lib/mogra-dl/.local/python-env/bin/pip3 install --upgrade --upgrade-strategy only-if-needed youtube-dl
  13. /var/lib/mogra-dl/.local/python-env/bin/pip3 install --upgrade --upgrade-strategy only-if-needed streamlink
  14. mkdir -p /var/lib/mogra-dl/twitch/{mogra,anisphere,anime_mogra,}
  15. mkdir -p /var/lib/mogra-dl/ustream/{mogra1,}
  16.  
  17. echo '#!/bin/bash
  18. while true; do
  19.  datetime="$(TZ='Asia/Tokyo' date +"%Y-%m-%d_%H-%M-%S")"
  20.  # download stream audio
  21.  /var/lib/mogra-dl/.local/python-env/bin/youtube-dl -ci --quiet -o ${datetime}.m4a --extract-audio --audio-quality 0 --no-part https://twitch.tv/$1
  22.  sleep 30
  23. done
  24. ' > /var/lib/mogra-dl/twitch/dl.sh
  25. chmod +x /var/lib/mogra-dl/twitch/dl.sh
  26.  
  27. echo '#!/bin/bash
  28. for video in "*.mp4" ; do
  29.  if [[ -f "$video" ]]; then
  30.    # extract audio
  31.    ffmpeg -i "$video" -vn -c:a copy "$(echo $video | sed 's/mp4/m4a/')"
  32.    rm "$video"
  33.  fi
  34. done
  35. while true; do
  36.  datetime="$(TZ='Asia/Tokyo' date +"%Y-%m-%d_%H-%M-%S")"
  37.  # download stream video
  38.  /var/lib/mogra-dl/.local/python-env/bin/streamlink http://ustream.tv/channel/$1 best -o "${datetime}.mp4" -f
  39.  if [[ -f "${datetime}.mp4" ]]; then
  40.    # extract audio
  41.    ffmpeg -i "${datetime}.mp4" -vn -c:a copy "${datetime}.m4a"
  42.    rm "${datetime}.mp4"
  43.  else
  44.    sleep 30
  45.  fi
  46. done
  47. ' > /var/lib/mogra-dl/ustream/dl.sh
  48. chmod +x /var/lib/mogra-dl/ustream/dl.sh
  49.  
  50. echo '#!/bin/bash
  51. cd "$(pwd)"
  52. for dir in /var/lib/mogra-dl/{twitch,ustream}/* ; do
  53.  if [[ -d "$dir" ]]; then
  54.    channel_name="$(basename $dir)"
  55.    cd "$dir"
  56.    ../dl.sh "$channel_name" &
  57.    cd - > /dev/null
  58.  fi
  59. done
  60. wait
  61. ' > /var/lib/mogra-dl/mogra-dl.sh
  62. chmod +x /var/lib/mogra-dl/mogra-dl.sh
  63.  
  64. exit
  65.  
  66. sudo echo '# Update youtube-dl and streamlink daily, reporting to syslog
  67. 00 00 * * * mogra-dl       /var/lib/mogra-dl/.local/python-env/bin/pip3 install --upgrade --upgrade-strategy only-if-needed youtube-dl 2>&1 | /usr/bin/logger -t mogra-dl-update
  68. 02 00 * * * mogra-dl       /var/lib/mogra-dl/.local/python-env/bin/pip3 install --upgrade --upgrade-strategy only-if-needed streamlink 2>&1 | /usr/bin/logger -t mogra-dl-update
  69. #
  70. ' > /etc/cron.d/mogra-dl-update
  71.  
  72. sudo echo '[Unit]
  73. After=network.target
  74.  
  75. [Service]
  76. WorkingDirectory=/var/lib/mogra-dl
  77. ExecStart=/var/lib/mogra-dl/mogra-dl.sh
  78. User=mogra-dl
  79. Group=mogra-dl
  80. Restart=always
  81. StandardOutput=syslog
  82. StandardError=syslog
  83. SyslogIdentifier=mogra-dl
  84.  
  85. [Install]
  86. WantedBy=multi-user.target
  87. ' > /etc/systemd/system/mogra-dl.service
  88.  
  89. sudo systemctl daemon-reload
  90. sudo systemctl enable mogra-dl.service
  91. sudo service mogra-dl start
RAW Paste Data Copied