前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【直播】使用python控制b站直播视频

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

作者头像
一朵灼灼华
发布2022-08-05 21:12:28
3.1K0
发布2022-08-05 21:12:28
举报
介绍

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

代码
代码语言:javascript
复制
#!/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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-05-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 介绍
  • 代码
  • 数据库
相关产品与服务
云点播
面向音视频、图片等媒体,提供制作上传、存储、转码、媒体处理、媒体 AI、加速分发播放、版权保护等一体化的高品质媒体服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档