python+ffmpeg实现推送本地音频/视频/本地话筒/本地桌面等到RTMP服务器

流媒体服务器搭建:https://blog.csdn.net/qq_21454973/article/details/102894919

上代码:

import os
os.environ['path'] = os.environ.get('path')+';C:\\tools'
import subprocess


# 将日志输出的时间类型转换成秒
def get_seconds(time):
    h = int(time[0:2])
    # print("时:" + str(h))
    m = int(time[3:5])
    # print("分:" + str(m))
    s = int(time[6:8])
    # print("秒:" + str(s))
    ms = int(time[9:12])
    # print("毫秒:" + str(ms))
    ts = (h * 60 * 60) + (m * 60) + s + (ms / 1000)
    return ts


# size=   25189kB time=00:04:28.67 bitrate= 768.0kbits/s speed= 748x
# video:0kB audio:25189kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000302%
#'-keyint_min', '25','-g', '25', 关键帧
# FrontMic (Realtek High Definiti
cmd = ['ffmpeg', '-re','-i', '''C:\\1.mp3''', '-vn', '-acodec', 'copy',
       '-f', 'flv', 'rtmp://192.168.1.3:1935/rtmp/test']




cmd = ['ffmpeg', '-re','-i', '''C:\\source\\p2tl4ij80h.mp4''', '-vcodec', 'copy', '-acodec', 'copy','-r','60',
       '-f', 'flv', 'rtmp://192.168.1.3:1935/rtmp/pjk516b3']
process = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, encoding="utf-8",
                           text=True)
# process = subprocess.Popen('''ffmpeg -f dshow -i audio="FrontMic (Realtek High Definiti" -f flv rtmp://192.168.1.3:1935/rtmp/test''', shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, encoding="utf-8",
#                            text=True)
for line in process.stdout:
    print(line)

    # duration_res = re.search(r'\sDuration: (?P<duration>\S+)', line)
    # if duration_res is not None:
    #     duration = duration_res.groupdict()['duration']
    #     duration = re.sub(r',', '', duration)
    #
    # result = re.search(r'\stime=(?P<time>\S+)', line)
    # if result is not None:
    #     elapsed_time = result.groupdict()['time']
    #     # 此处可能会出现进度超过100%,未对数值进行纠正
    #     progress = (get_seconds(elapsed_time) / get_seconds(duration)) * 100
    #     print(elapsed_time)
    #     print(progress)
    #     print("进度:%3.2f" % progress + "%")
process.wait()
if process.poll() == 0:
    print("success:", process)
else:
    print("error:", process)

  推送音频:

cmd = ['ffmpeg', '-re','-i', '''C:\\1.mp3''', '-vn', '-acodec', 'copy',
       '-f', 'flv', 'rtmp://192.168.1.3:1935/rtmp/test']

  推送视频:

cmd = ['ffmpeg', '-re','-i', '''C:\\source\\p2tl4ij80h.mp4''', '-vcodec', 'copy', '-acodec', 'copy','-r','60',
       '-f', 'flv', 'rtmp://192.168.1.3:1935/rtmp/pjk516b3']

  推送话筒:

process = subprocess.Popen('''ffmpeg -f dshow -i audio="FrontMic (Realtek High Definiti" -f flv rtmp://192.168.1.3:1935/rtmp/test''', shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, encoding="utf-8",
                          text=True)

  FrontMic (Realtek High Definiti 是可推送设备名称,可参考:

https://www.cnblogs.com/wohuiyijiu/p/12808957.html

推送桌面需要安装一个录屏软件【Screen Capturer Recorder】自行百度下载

命令:
ffmpeg  -f dshow -i video="screen-capture-recorder":audio="virtual-audio-capturer"  -f flv rtmp://192.168.1.3:1935/rtmp/test

替换下命令即可

告辞!

 

posted @ 2020-04-30 15:42  我惠依旧  阅读(3828)  评论(1编辑  收藏  举报