stream_desktop 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env python2
  2. #
  3. # Uses VLC to stream desktop to raspberry pi
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. import argparse
  18. import logging
  19. import os
  20. import sys
  21. import subprocess
  22. import socket
  23. import time
  24. import paramiko
  25. PI_IP = os.getenv('PI_IP', 'pi.tyler.zone')
  26. logging.basicConfig(level=logging.INFO, format='%(asctime)-8s %(message)s')
  27. ssh_creds = {
  28. 'hostname': PI_IP,
  29. 'username': 'pi',
  30. 'pkey': paramiko.RSAKey.from_private_key_file(
  31. os.path.join(os.getenv('HOME'), '.ssh/id_rsa')
  32. ),
  33. }
  34. def get_ip():
  35. return [(s.connect(('8.8.8.8', 80)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]
  36. def connect_ssh():
  37. client = paramiko.SSHClient()
  38. client.load_host_keys(os.path.join(os.getenv('HOME'), '.ssh/known_hosts'))
  39. client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  40. client.connect(**ssh_creds)
  41. return client
  42. def start_stream():
  43. ssh = connect_ssh()
  44. stdin, stdout, stderr = ssh.exec_command('omxplayer -z http://%s:8080/stream.mp4' % get_ip())
  45. logging.info('Streaming live, sonny-jim...')
  46. def youtube_it(url):
  47. url= subprocess.check_output('youtube-dl -g "%s"' % url, shell=True)
  48. ssh = connect_ssh()
  49. cmd = 'omxplayer -z "%s"' % url.strip()
  50. stdin, stdout, stderr = ssh.exec_command(cmd)
  51. def main():
  52. p = argparse.ArgumentParser()
  53. p.add_argument('-u', '--url', required=False, help='url to play')
  54. args = vars(p.parse_args())
  55. if args['url']:
  56. youtube_it(args['url'])
  57. else:
  58. try:
  59. vlc_cmd = ['cvlc', 'screen://', '--screen-fps=12', '--screen-caching=100', '--sout']
  60. vlc_cmd.append('"#transcode{vcodec=h264,venc=x264{keyint=12,scenecut=80,' \
  61. 'profile=faster,intra-refresh,tune=zerolatency,bframes=0,' \
  62. 'nocabac},fps=12,scale=0.75,vb=512}:std{access=http,' \
  63. 'mux=ts,dst=/stream.mp4}"')
  64. proc = subprocess.Popen(' '.join(vlc_cmd),
  65. stderr=subprocess.PIPE,
  66. stdout=subprocess.PIPE,
  67. shell=True)
  68. logging.info('vlc pid [%s]' % proc.pid)
  69. start_stream()
  70. proc.wait()
  71. except KeyboardInterrupt:
  72. proc.terminate()
  73. if __name__ == '__main__':
  74. main()