【直播】使用python控制b站直播视频

By Heanny
2020-05-04
2145 read

介绍

通过python控制数据库,操作ffmpeg导出视频流到b站直播内容。
简单代码,可能会存在瑕疵,测试4天无严重bug

代码

#!/usr/bin/env python
# Time-stamp: <2020-05-02 17:31:22 Monday by Heanny>
# Auth: Heanny<lzh@heanny.cn>
import os
import time

import pymysql

rtmp = 'rtmp://js.live-send.acg.tv/live-js/'
code = '?streamname=***&key=***'
dir = "/mnt/ssd/video/movie/"
nameList = []


class sqlData:
    def __init__(self):
        self.host = '127.0.0.1'
        self.user = 'root'
        self.pwd = 'root'
        self.table = 'heanny'
        self.db = pymysql.connect(self.host, self.user, self.pwd, self.table)
        self.db.set_charset('utf8')
        self.cursor = self.db.cursor()

    def _query(self, sql, isAll=True):
        self.cursor.execute(sql)
        data = self.cursor.fetchall() if isAll else self.cursor.fetchone()
        return data

    def _delete(self, sql):
        self.cursor.execute(sql)
        self.db.commit()
        return True

    def _insert(self, sql, data):
        self.cursor.executemany(sql, data)
        self.db.commit()
        return True

    def _close(self):
        self.cursor.close()
        self.db.close()


def getIsLive():
    host = '127.0.0.1'
    user = 'root'
    pwd = 'root'
    table = 'heanny'
    db = pymysql.connect(host, user, pwd, table)
    db.set_charset('utf8')
    cursor = db.cursor()
    sql = 'select value from  bilibili_config where name = "isLive"'
    cursor.execute(sql)
    data = cursor.fetchone()
    cursor.close()
    db.close()
    return data[0] if data else None


def getList():
    s = sqlData()
    tops = list(
        map(lambda x: {'id': x[0], 'title': x[1], 'path': x[2]}, s._query('select id,title,path from  bilibili_list order by id')))
    if not tops:
        defaults = s._query('select id,title,path from  bilibili_live where bilibili_live.show = 1')
        print('Live list updated ')
        sql = "INSERT INTO bilibili_list(lid, title, path) VALUES (%s,%s,%s)"
        s._insert(sql, defaults)
        tops = list(map(lambda x: {'id': x[0], 'title': x[1], 'path': x[2]},
                        s._query('select id,title,path from  bilibili_list  order by id')))
    reData = tops[0]
    s._delete('update `bilibili_config` set value = "{}" where name = "now"'.format(reData['title']))
    s._delete('insert into `bilibili_history`(path) values ("{}")'.format(reData['path']))
    s._delete('DELETE FROM `bilibili_list` WHERE id = "{}"'.format(reData['id']))
    s._close()
    return reData


def main():
    isLive = getIsLive()
    while isLive:
        data = getList()
        # -b:a 192k
        cmd = 'ffmpeg -re -i "{}{}" -vcodec copy -acodec aac  -f flv "{}{}"'.format(dir, data['path'], rtmp, code)
        os.system(cmd)
        isLive = getIsLive()
        time.sleep(3)


if __name__ == '__main__':
    main()
    # data,isLive = getList()
    # print(data,isLive)

数据库

配置bilibili_config:
isLive能够控制是否进行播放视频
now是正在播放的电影名

播放列表bilibili_list:
该表为即将播放的列表,如果为空,则自动添加点播列表所有内容

点播列表bilibili_live:
该表格能够点播,点播内容添加至播放列表。
show控制是否显示到列表

点播平台:http://live.heanny.cn

Nginx访问日志中UserAgent的一些参考建议
【直播】nginx搭建rtmp流直播环境

Comments

暂无评论,还不快来坐沙发...

Leave a Reply