Last active
December 18, 2022 10:00
-
-
Save weedge/ee9c99baffac998123cb27ea083dec0c to your computer and use it in GitHub Desktop.
video stream to server socket, play this stream from socket (use VLC in MAC)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
# For Mac: alias vlc='/Applications/VLC.app/Contents/MacOS/VLC' | |
cmdline = '/Applications/VLC.app/Contents/MacOS/VLC --demux h264 -' | |
#cmdline = 'mplayer -fps 31 -cache 1024 -' | |
player = subprocess.Popen(cmdline.split(), 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment