Python和FFMPEG视频流不通过HTML5显示。

时间:2023-02-08 23:47:07

I'm trying to write a python script to serve a video over HTTP and display it via HTML5 video tag, I'm using FFMPEG to serve the video over HTTP and receiving the the video via sockets in Python. FFMPEG seems to be sending the video and my Python script is receiving it but for some reason I'm not able to display it in my web browser nor getting any visible error in my script.

我正在尝试编写一个python脚本来通过HTTP提供视频并通过HTML5视频标记显示它,我正在使用FFMPEG来通过HTTP提供视频,并通过python中的套接字接收视频。FFMPEG似乎正在发送视频,我的Python脚本正在接收它,但是出于某种原因,我无法在web浏览器中显示它,也无法在脚本中获得任何可见的错误。

Any help will be highly appreciated.

非常感谢您的帮助。

This is the FFMPEG line I'm using to send the video to HTTP

这是我用来发送视频到HTTP的FFMPEG线。

FFMPEG -re -i video_file.webm -c:v libx264 -c:a copy -f h264 http://127.0.0.1:8081

Here is my Python code

这是我的Python代码

import socket   #for sockets handling
import time     #for time functions
import sys

hostIP = '127.0.0.1'
SourcePort = 8081 #FFMPEG
PlayerPort = 8082 #Internet Browser

def gen_headers():
     # determine response code
     h = ''
     h = 'HTTP/1.1 200 OK\n'
     # write further headers
     current_date = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
     h += 'Date: ' + current_date +'\n'
     h += 'Content-Type: video/mp4\n\n'
     return h

def start_server():
    socketFFMPEG = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # this is for easy starting/killing the app
    socketFFMPEG.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    print('Socket created')

    try:
        socketFFMPEG.bind((hostIP, SourcePort))
        print('Socket bind complete')
    except socket.error as msg:
        print('Bind failed. Error : ' + str(sys.exc_info()))
        sys.exit()

    #Start listening on socketFFMPEG
    socketFFMPEG.listen(10)
    print('Socket now listening. Waiting for video source from FFMPEG on port', SourcePort)

    conn, addr = socketFFMPEG.accept()
    ip, port = str(addr[0]), str(addr[1])
    print('Accepting connection from ' + ip + ':' + port)

    socketPlayer = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    socketPlayer.bind((hostIP, PlayerPort))
    socketPlayer.listen(1) #listen just 1 petition
    print('Waiting for Internet Browser')
    conn2, addr2 = socketPlayer.accept()
    conn2.sendall(gen_headers().encode())

    while True:
        try :
            #receive data from client FFMPEG
            input_from_FFMPEG = conn.recv(1024) 
            #send data to internet browser
            conn2.sendall(input_from_FFMPEG)
        except socket.error:
            print('Error data :' + str(input_from_FFMPEG))
            print('send Error : ' + str(sys.exc_info()))
            conn2.close()
            sys.exit()

    socketFFMPEG.close()

start_server()  

I'm getting the error 10053 "An established connection was aborted by the software in your host machine" when loading the following "byte" type data

我得到的错误是10053“在加载以下“字节”类型数据时,您的主机上的软件终止了一个已建立的连接”

\x00\x00\x00\x01A\x9b\x1dI\xe1\x0f&S\x02\xbf\xb1\x82j3{qz\x85\xca\\\xb2\xb7\xc5\xdfi\x92y\x0c{\xb0\xde\xd1\x96j\xccE\xa3G\x87\x84Z\x0191\xba\x8a3\x8e\xe2lfX\x82\xd4*N\x8a\x9f\xa9\xc9\xfb\x13\xfc_]D\x0f\x9e\x1c"\x0fN\xceu\t\x17n\xbe\x95\xd1\x10Wj\xf5t\x90\xa8\x1am\xf7!d\x82py\n\x10\xf5\x9b{\xd9\xf8\x8e^\xc7\xb3o+\x0eQX\xb3\x17B?\xb8\x1c\xecP\xa0\xf10\xc7\xc8\x8d\xf1P\xd3\xdf\xd0\xd5\x13ah+bM\x9c\xbe\xca\xb4\x9a?\xac\xb9\x0fao\xf3\xed\x9c\xe4^\x10\x079t\xf4\x0f\xce\xbe*\xd4w\x1f\x1a\x02\xbd\xed\xe9\x16\x8a\x98\xe0\x1d\xc4\xde5\xa8\xf0\x88\xb4\x07=\xe2w\xc3Q\xc1\x99K7\xff\x01`(\xb3sN\x88\x18\xfd7\xd4\x07\xab\x95\xf95\x05\xcd\xd6,!=\xfb\xc4\xc8\xbf\xad\x96\x83\xc0\x9b%\xdds\x92s\xc0lN\xdd\x14\xba\xbd\x04L\xb1\x08\xec[~tB~`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843\xa3\xfb\xea\xc9d\x05\xeb]

1 个解决方案

#1


0  

With this line:

这条线:

conn2.sendall(gen_headers().encode()+input_from_FFMPEG)

your code repeatedly sends the HTTP headers once for every 1024 bytes of video. You only need to send the headers once after the GET request is received. Then send the body which will be the video data.

您的代码每1024字节重复发送一次HTTP报头。您只需要在收到GET请求后发送一次header。然后发送将是视频数据的主体。

Also, read the request before replying: conn2.recv(1024). I've found this to be necessary, however, I am not using Windows.

另外,在回复之前请阅读请求:conn2.recv(1024)。我发现这是必要的,然而,我没有使用Windows。

One other thing, The HTTP headers are supposed to be separated by \r\n, not just \n, so try changing that if the above doesn't fix it.

还有一件事,HTTP报头应该用\r\n来分隔,而不仅仅是\n,所以如果上面的方法不能解决这个问题,请尝试修改它。

#1


0  

With this line:

这条线:

conn2.sendall(gen_headers().encode()+input_from_FFMPEG)

your code repeatedly sends the HTTP headers once for every 1024 bytes of video. You only need to send the headers once after the GET request is received. Then send the body which will be the video data.

您的代码每1024字节重复发送一次HTTP报头。您只需要在收到GET请求后发送一次header。然后发送将是视频数据的主体。

Also, read the request before replying: conn2.recv(1024). I've found this to be necessary, however, I am not using Windows.

另外,在回复之前请阅读请求:conn2.recv(1024)。我发现这是必要的,然而,我没有使用Windows。

One other thing, The HTTP headers are supposed to be separated by \r\n, not just \n, so try changing that if the above doesn't fix it.

还有一件事,HTTP报头应该用\r\n来分隔,而不仅仅是\n,所以如果上面的方法不能解决这个问题,请尝试修改它。