Skip to content

Instantly share code, notes, and snippets.

@alexander480
Created July 15, 2018 23:35
Show Gist options
  • Select an option

  • Save alexander480/da9678f5b9795b83e05ebe61ef04f5b8 to your computer and use it in GitHub Desktop.

Select an option

Save alexander480/da9678f5b9795b83e05ebe61ef04f5b8 to your computer and use it in GitHub Desktop.

Revisions

  1. alexander480 created this gist Jul 15, 2018.
    28 changes: 28 additions & 0 deletions NetworkStreamCam.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    import socket
    import subprocess

    # Start a socket listening for connections on 0.0.0.0:8000 (0.0.0.0 means
    # all interfaces)
    server_socket = socket.socket()
    server_socket.bind(('0.0.0.0', 8000))
    server_socket.listen(0)

    # Accept a single connection and make a file-like object out of it
    connection = server_socket.accept()[0].makefile('rb')
    try:
    # Run a viewer with an appropriate command line. Uncomment the mplayer
    # version if you would prefer to use mplayer instead of VLC
    cmdline = ['vlc', '--demux', 'h264', '-']
    #cmdline = ['mplayer', '-fps', '25', '-cache', '1024', '-']
    player = subprocess.Popen(cmdline, stdin=subprocess.PIPE)
    while True:
    # Repeatedly read 1k of data from the connection and write it to
    # the media player's stdin
    data = connection.read(1024)
    if not data:
    break
    player.stdin.write(data)
    finally:
    connection.close()
    server_socket.close()
    player.terminate()