Last active
December 25, 2015 03:28
-
-
Save h4/6909581 to your computer and use it in GitHub Desktop.
Скрипт rtmp-транслятора
This file contains hidden or 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
#!/usr/bin/env python | |
import gi | |
import time | |
import signal | |
import sys | |
import ConfigParser | |
import RPi.GPIO as GPIO | |
gi.require_version("Gst", "1.0") | |
from gi.repository import Gst | |
from gi.repository import GLib | |
Gst.init(None) | |
class Controller(object): | |
def __init__(self, Robot, config_file=None): | |
GPIO.setmode(GPIO.BCM) | |
GPIO.cleanup() | |
self.robot = Robot(config_file) | |
self.led = 23 | |
self.btn = 25 | |
self.runned = False | |
GPIO.setup(self.led, GPIO.OUT) | |
GPIO.output(self.led, self.runned) | |
GPIO.setup(self.btn, GPIO.IN) | |
self.setup_btn() | |
def setup_btn(self): | |
btn = self.btn | |
def callback(elem): | |
if not self.runned: | |
GPIO.output(self.led,True) | |
self.robot.run() | |
else: | |
GPIO.output(self.led, False) | |
self.robot.pause() | |
self.runned = not self.runned | |
GPIO.add_event_detect(self.btn, GPIO.RISING, callback=callback) | |
class Bender(object): | |
options = { | |
'Video': { | |
'width': 640, | |
'height': 480, | |
'framerate': 15, | |
'bitrate': 1000000, | |
}, | |
'Audio': { | |
'device': 'plughw:1', | |
'channels': 1, | |
'bitrate_in': 48000, | |
'bitrate_out': 48000, | |
}, | |
'Stream': { | |
'host': 'rtmp://rtmp.brnv.ru', | |
'path': '/mylive/win.flv', | |
} | |
} | |
def __init__(self, config_file=None): | |
if config_file: | |
config = ConfigParser.RawConfigParser() | |
config.read(config_file) | |
for section in self.options.keys(): | |
for param in self.options[section].keys(): | |
value = self._get_param(config, section, param) | |
if value is not None: | |
self.options[section][param] = value | |
def _get_param(self, config, section, param): | |
try: | |
if isinstance(self.options[section][param], int): | |
return config.getint(section, param) | |
else: | |
return config.get(section, param) | |
except ConfigParser.NoOptionError: | |
return None | |
def run(self): | |
self.pipeline = Gst.parse_launch("""v4l2src ! \ | |
video/x-raw,width={Video[width]},height={Video[height]},framerate={Video[framerate]}/1 ! \ | |
omxh264enc target-bitrate={Video[bitrate]} control-rate=variable ! \ | |
video/x-h264,profile=high ! \ | |
h264parse ! \ | |
queue ! \ | |
flvmux name=mux alsasrc device={Audio[device]} ! \ | |
audioresample ! \ | |
audio/x-raw,rate={Audio[bitrate_in]},channels={Audio[channels]} ! \ | |
queue ! \ | |
voaacenc bitrate={Audio[bitrate_out]} ! \ | |
aacparse !\ | |
queue ! \ | |
mux. mux. ! \ | |
rtmpsink location={Stream[host]}{Stream[path]}""".format(**self.options)) | |
self.bus = self.pipeline.get_bus() | |
self.bus.add_signal_watch() | |
self.bus_num = self.bus.connect('message::error', self.on_error) | |
self.pipeline.set_state(Gst.State.PLAYING) | |
def pause(self): | |
self.bus.disconnect(self.bus_num) | |
self.bus.remove_signal_watch() | |
self.pipeline.set_state(Gst.State.NULL) | |
def on_error(self, bus, msg): | |
print('on_error():', msg.parse_error()) | |
def signal_handler(signal, frame): | |
GPIO.cleanup() | |
sys.exit() | |
signal.signal(signal.SIGINT, signal_handler) | |
signal.signal(signal.SIGTERM, signal_handler) | |
if __name__ == '__main__': | |
controller = Controller(Bender, config_file='/boot/stream.cfg') | |
import threading | |
threading.Event().wait() | |
This file contains hidden or 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
#!/bin/bash | |
gst-launch-1.0 v4l2src ! \ | |
'video/x-raw,width=640,height=480,framerate=15/1' ! \ | |
omxh264enc target-bitrate=1000000 control-rate=variable ! \ | |
video/x-h264,profile=high ! \ | |
h264parse ! \ | |
queue ! \ | |
flvmux name=mux alsasrc device=plughw:1 ! \ | |
audioresample ! \ | |
audio/x-raw,rate=48000 ! \ | |
queue ! \ | |
voaacenc bitrate=32000 ! \ | |
aacparse !\ | |
queue ! \ | |
mux. mux. ! \ | |
rtmpsink location=rtmp://erlyvideo.avalon.ru/live/head.flv |
This file contains hidden or 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
[Video] | |
width=640 | |
height=480 | |
framerate=15 | |
bitrate=10000000 | |
[Audio] | |
device=plughw:1 | |
channels=1 | |
bitrate_in=48000 | |
bitrate_out=48000 | |
[Stream] | |
host=rtmp://rtmp.brnv.ru | |
path=/mylive/win.flv | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment