使用 aler9/rtsp-simple-server,能够从文件流式传输 HLS,但不能从 ffmpeg 的标准输入

问题描述 投票:0回答:0

我想在网页上实时显示以编程方式生成的流。为此,我有 rtsp-simple-server 启用了 RTSP 和 HLS。我设法使用命令

ffmpeg -re -stream_loop -1 -i file.mp4 -vcodec libx264 -f rtsp -rtsp_transport tcp rtsp://localhost:8554/mystream
从文件发布流,并在 http://localhost:8888/mystream 上看到它。

但是当我尝试对来自 ffmpeg 的标准输入的 python 执行相同操作时,我正在无限加载视频而不是流。这是我的代码:

import random
import shlex
import subprocess
import time

import numpy as np


def main():
    width = 1024
    height = 720
    framerate = 1
    frame_duration = 1 / framerate
    cmd = shlex.split(
        f'ffmpeg'
        f' -y'
        f' -f rawvideo'
        f' -vcodec rawvideo'
        f' -s {width}x{height}'
        f' -pix_fmt bgr24'
        f' -r {framerate}'
        f' -i -'
        f' -r {framerate}'
        f' -force_key_frames expr:eq(mod(n,3),0)'
        f' -vcodec libx264'
        f' -crf 18'
        f' -preset ultrafast'
        f' -tune zerolatency'
        f' -f rtsp'
        f' -rtsp_transport tcp'
        f' rtsp://localhost:8554/mystream'
    )
    ffmpeg_process = subprocess.Popen(cmd, stdin=subprocess.PIPE)
    try:
        while True:
            image = np.full(
                (height, width, 3),
                (
                    random.randint(0, 255),
                    random.randint(0, 255),
                    random.randint(0, 255),
                ),
                dtype=np.uint8,
            )
            ffmpeg_process.stdin.write(image.tobytes())
            time.sleep(frame_duration)
    finally:
        ffmpeg_process.stdin.close()
        ffmpeg_process.wait()


if __name__ == '__main__':
    main()

在服务器的日志中似乎没有错误消息而不是单个 404 状态(它只是关于 favicon,对吧?)

2023/03/25 13:50:32 DEB [HLS] [conn 172.18.0.1] GET /mystream/
2023/03/25 13:50:32 DEB [HLS] [conn 172.18.0.1] [c->s] GET /mystream/ HTTP/1.1
Host: localhost:8888
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: ru,en-US;q=0.7,en;q=0.3
Connection: keep-alive
Cookie: csrftoken=iRZDO5rsJzhh5peyyKhViN9yRslNQbuZ; Webstorm-713207de=ca4f5f1e-40e8-4bfd-97da-1be2f15f6e9e
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: cross-site
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0


2023/03/25 13:50:32 INF [HLS] [muxer mystream] created (requested by 172.18.0.1)
2023/03/25 13:50:32 INF [HLS] [muxer mystream] is converting into HLS, 1 track (H264)
2023/03/25 13:50:32 DEB [HLS] [conn 172.18.0.1] [s->c] HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Content-Type: text/html
Server: rtsp-simple-server

(body of 1240 bytes)
2023/03/25 13:50:32 DEB [HLS] [conn 172.18.0.1] GET /favicon.ico
2023/03/25 13:50:32 DEB [HLS] [conn 172.18.0.1] [c->s] GET /favicon.ico HTTP/1.1
Host: localhost:8888
Accept: image/avif,image/webp,*/*
Accept-Encoding: gzip, deflate, br
Accept-Language: ru,en-US;q=0.7,en;q=0.3
Connection: keep-alive
Cookie: csrftoken=iRZDO5rsJzhh5peyyKhViN9yRslNQbuZ; Webstorm-713207de=ca4f5f1e-40e8-4bfd-97da-1be2f15f6e9e
Referer: http://localhost:8888/mystream/
Sec-Fetch-Dest: image
Sec-Fetch-Mode: no-cors
Sec-Fetch-Site: same-origin
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0


2023/03/25 13:50:32 DEB [HLS] [conn 172.18.0.1] [s->c] HTTP/1.1 404 Not Found
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Server: rtsp-simple-server


2023/03/25 13:50:32 DEB [HLS] [conn 172.18.0.1] GET /mystream/index.m3u8
2023/03/25 13:50:32 DEB [HLS] [conn 172.18.0.1] [c->s] GET /mystream/index.m3u8 HTTP/1.1
Host: localhost:8888
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: ru,en-US;q=0.7,en;q=0.3
Connection: keep-alive
Cookie: csrftoken=iRZDO5rsJzhh5peyyKhViN9yRslNQbuZ; Webstorm-713207de=ca4f5f1e-40e8-4bfd-97da-1be2f15f6e9e
Referer: http://localhost:8888/mystream/
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-origin
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0


2023/03/25 13:50:32 DEB [HLS] [conn 172.18.0.1] [s->c] HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Content-Type: application/x-mpegURL
Server: rtsp-simple-server

(body of 122 bytes)
2023/03/25 13:50:34 DEB [HLS] [conn 172.18.0.1] GET /mystream/index.m3u8
2023/03/25 13:50:34 DEB [HLS] [conn 172.18.0.1] [c->s] GET /mystream/index.m3u8 HTTP/1.1
Host: localhost:8888
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: ru,en-US;q=0.7,en;q=0.3
Connection: keep-alive
Cookie: csrftoken=iRZDO5rsJzhh5peyyKhViN9yRslNQbuZ; Webstorm-713207de=ca4f5f1e-40e8-4bfd-97da-1be2f15f6e9e
Referer: http://localhost:8888/mystream/
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-origin
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0


2023/03/25 13:50:34 DEB [HLS] [conn 172.18.0.1] [s->c] HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Content-Type: application/x-mpegURL
Server: rtsp-simple-server

(body of 122 bytes)
2023/03/25 13:50:36 DEB [HLS] [conn 172.18.0.1] GET /mystream/index.m3u8
2023/03/25 13:50:36 DEB [HLS] [conn 172.18.0.1] [c->s] GET /mystream/index.m3u8 HTTP/1.1
Host: localhost:8888
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: ru,en-US;q=0.7,en;q=0.3
Connection: keep-alive
Cookie: csrftoken=iRZDO5rsJzhh5peyyKhViN9yRslNQbuZ; Webstorm-713207de=ca4f5f1e-40e8-4bfd-97da-1be2f15f6e9e
Referer: http://localhost:8888/mystream/
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-origin
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0


2023/03/25 13:50:36 DEB [HLS] [conn 172.18.0.1] [s->c] HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Content-Type: application/x-mpegURL
Server: rtsp-simple-server

(body of 122 bytes)

如何让我的 python 流在网页上可见?

python ffmpeg http-live-streaming rtsp
© www.soinside.com 2019 - 2024. All rights reserved.