Close Menu
Techs Slash

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    What's Hot

    The Importance of Audiology Equipment Calibration: Ensuring Accurate Hearing Assessments

    July 6, 2025

    Fintech Fixes: How Technology Is Changing the Way Americans Handle Poor Credit

    July 3, 2025

    From Electrification to Everyday Ops: How Digital Marketplaces Drive Value for Enterprises

    July 3, 2025
    Facebook X (Twitter) Instagram
    Techs Slash
    • Home
    • News
      • Tech
      • Crypto News
      • Cryptocurrency
    • Entertainment
      • Actors
      • ANGEL NUMBER
      • Baby Names
      • Beauty
      • beauty-fashion
      • facebook Bio
      • Fitness
      • Dubai Tour
    • Business
      • Business Names
    • Review
      • Software
      • Smartphones & Apps
    • CONTRIBUTION
    Facebook X (Twitter) Instagram
    Techs Slash
    Home»python»Python 3 FFMPEG Script to Convert BLOB Data to RTMP Live Video Streaming in Browser Using HTML5
    python

    Python 3 FFMPEG Script to Convert BLOB Data to RTMP Live Video Streaming in Browser Using HTML5

    Ranveer KumarBy Ranveer KumarOctober 1, 2022No Comments2 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email

    Warning: Trying to access array offset on value of type bool in /home/cadesimu/techsslash.com/wp-content/themes/smart-mag/partials/single/featured.php on line 78
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Python 3 FFMPEG Script to Convert BLOB Data to RTMP Live Video Streaming in Browser Using HTML5

    #! /usr/bin/env python3.6 import argparseimport loggingfrom asyncio import get_event_loop import asyncioimport socketio from aiohttp import webfrom subprocess import Popen, PIPE, DEVNULL sio = socketio.AsyncServer()logger = logging.getLogger('transfer-logger')server = None  class FFMPEGProcess:    DEFAULT_LIFE = 6    LIFE_STEP = 3     ffmpeg = None    rtmp_url = None    options = None    key = None     def __init__(self, key: str, rtmp_url: str):        self.life = self.DEFAULT_LIFE         self.key = key        self.rtmp_url = rtmp_url        self.options = [            'ffmpeg', '-vcodec', 'libvpx', '-i', '-',            '-c:v', 'libx264', '-preset', 'veryfast', '-tune', 'zerolatency',            '-an', '-bufsize', '1000', '-f', 'flv', rtmp_url        ]     async def run(self):        self.ffmpeg = await asyncio.create_subprocess_exec(            *self.options,            stdin=asyncio.subprocess.PIPE, loop=server.loop, stdout=DEVNULL, stderr=DEVNULL        )     async def stop(self):        try:            self.ffmpeg.kill()            await self.ffmpeg.wait()        except AttributeError:            pass     def set_default_life(self):        self.life = self.DEFAULT_LIFE     def is_life(self):        return self.life > 0  class Server:    def __init__(self, host: str = None, port: int = None):        assert isinstance(host, str)        assert isinstance(port, int)         self.host = host        self.port = port        self.ffmpeg_processes = {}        self.loop = get_event_loop()     def run(self):        logger.info(f'Run server on {self.host}:{self.port}')         self.app = web.Application(loop=self.loop)        self.app['websockets'] = []         sio.attach(self.app)         self.loop.create_task(self.watch_processes())        web.run_app(self.app, host=self.host, port=self.port)     async def watch_processes(self):        while True:            for process in self.ffmpeg_processes.values():                try:                    process.life -= process.LIFE_STEP                    if not process.is_life():                        self.stop_process(process.key)                except TypeError:                    pass             await asyncio.sleep(FFMPEGProcess.LIFE_STEP, loop=self.loop)     def add_process(self, process: FFMPEGProcess):        self.ffmpeg_processes[process.key] = process     async def stop_process(self, key: str):        try:            await self.ffmpeg_processes[key].stop()            del self.ffmpeg_processes[key]        except KeyError:            pass     @staticmethod    def check_ffmpeg():        logger.info('Check ffmpeg  ... ')        command = 'ffmpeg -h'        proc = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)        proc.wait()         assert proc.returncode == 0, 'ffmpeg is not installed'     @staticmethod    def parse_args():        parser = argparse.ArgumentParser()        parser.add_argument('host', type=str, help='running host')        parser.add_argument('port', type=int, help='running port')         args = parser.parse_args()         return args.host, args.port  @sio.on('start')async def connect(key, url):    process = FFMPEGProcess(key, url)    server.add_process(process)     await process.run()  @sio.on('stream')async def message(key, data):    try:        process = server.ffmpeg_processes[key]        process.ffmpeg.stdin.write(data)        process.life = process.set_default_life()    except (KeyError, AttributeError):        pass  @sio.on('disconected')async def disconected(key):    server.stop_process(key)  if __name__ == '__main__':    args = Server.parse_args()     server = Server(*args)    server.check_ffmpeg()    server.run()
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Ranveer Kumar
    • Website

    Related Posts

    JSONDecodeError: Expecting value: line 1 column 1 (char 0)

    December 18, 2023

    Why does math.log result in ValueError: math domain error?

    December 17, 2023

    “inconsistent use of tabs and spaces in indentation” [duplicate]

    December 16, 2023
    Leave A Reply Cancel Reply

    Top Posts

    Sapne Me Nahane Ka Matlab

    March 18, 2024

    Sapne Me Nagn Stri Dekhna

    March 18, 2024

    Self Reliance: Release Date, Cast, Plot, Trailer, and More Information

    March 18, 2024

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    ABOUT TECHSSLASH

    Welcome to Techsslash! We're dedicated to providing you with the best of technology, finance, gaming, entertainment, lifestyle, health, and fitness news, all delivered with dependability.

    Our passion for tech and daily news drives us to create a booming online website where you can stay informed and entertained.

    Enjoy our content as much as we enjoy offering it to you

    Most Popular

    Sapne Me Nahane Ka Matlab

    March 18, 2024

    Sapne Me Nagn Stri Dekhna

    March 18, 2024

    Self Reliance: Release Date, Cast, Plot, Trailer, and More Information

    March 18, 2024
    CONTACT DETAILS

    Phone: +92-302-743-9438
    Email: contact@serpinsight.com

    Our Recommendation

    Here are some helpfull links for our user. hopefully you liked it.

    Techs Slash
    Facebook X (Twitter) Instagram Pinterest
    • Home
    • About us
    • contact us
    • Affiliate Disclosure
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • Write for us
    • Daman Game
    © 2025 Techsslash. All Rights Reserved

    Type above and press Enter to search. Press Esc to cancel.