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

替換下命令即可

告辭!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM