Skip to content

Instantly share code, notes, and snippets.

@ljakob
Created May 14, 2021 10:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ljakob/e38d8cfcc9efb99c204f62105c6f4c60 to your computer and use it in GitHub Desktop.
Save ljakob/e38d8cfcc9efb99c204f62105c6f4c60 to your computer and use it in GitHub Desktop.
ffmpeg encoder for "Megapixel Camera"
#!/usr/bin/python3
import sys
import subprocess
import logging
import socket
import time
HOST = '10.247.101.2' # The server's hostname or IP address
PORT = 90 # The port used by the server
OUTPUT_PATH = '/tmp/HLS/stream'
FFMPEG = """
exec \
ffmpeg -fflags nobuffer -i - -vsync 0 -copyts \
-vcodec copy \
-movflags frag_keyframe+empty_moov \
-an \
-f segment \
-segment_list_flags live \
-segment_time 1 \
-segment_list_size 3 \
-segment_format mpegts \
-segment_list /tmp/HLS/index.m3u8 \
-segment_list_type m3u8 \
-segment_list_entry_prefix '' \
-segment_wrap 5 \
/tmp/HLS/%d.ts
"""
log = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)
PACKET1 = (
0x49, 0x4e, 0x46, # INF
0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
0x80, # length of following block
0x61, 0x64, 0x6d, 0x69, 0x6e, # username
0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00
)
PACKET2 = (
0x49, 0x4e, 0x46, # INF
0x03, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
0x18, # length of following block
0x04, 0x03, 0x04, 0x00, # has some meaning
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00 )
def runSocketChannel() -> None:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.sendall(bytes(PACKET1))
data = s.recv(1024)
print('Received', repr(data))
s.sendall(bytes(PACKET2))
data = s.recv(36) # read 36 bytes only (header)
inf = data[0:3]
log.debug('INF=%s', inf)
if inf != b'INF':
log.error('bad response from socket')
return
args = ['/bin/sh', '-c', FFMPEG]
status = subprocess.run(args, stdin=s)
log.info('ffmpeg returned %s', status)
def main():
log.debug('starting')
while True:
try:
runSocketChannel()
except:
log.exception('exception from socket')
log.info('10 secs delay')
time.sleep(10)
log.info('restart')
main()
@sperglord8008s
Copy link

sperglord8008s commented Aug 28, 2021

Good Stuff thanks for the effort its become quite useful to me , Did you ever find out what the addresses on line 63 do?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment