[英]How would I go about playing a video stream with ffpyplayer?
First time poster here, so go easy on me.第一次海报在这里,所以放轻松。
I'm working on a fun little project for myself and friends, basically I want to be able to stream and recieve video using ffmpeg, as a sort of screen sharing application.我正在为自己和朋友们做一个有趣的小项目,基本上我希望能够使用 ffmpeg 流式传输和接收视频,作为一种屏幕共享应用程序。 I'm a complete python noob and im just going off of the documentation for each.
我是一个完整的 python 菜鸟,我只是离开了每个人的文档。 Heres what I have for sending:
这是我要发送的内容:
import ffmpeg
stream = ffmpeg.input("video.mp4")
stream = ffmpeg.output(stream, "tcp://127.0.0.1:1234", format="mpegts")
ffmpeg.run(stream)
It's simple but it works, when I run ffplay.exe -i tcp://127.0.0.1:1234?listen -hide_banner
in a command prompt and run the code to send the video, it works perfectly, but when I try and use my code to recieve a video, all I get is audio, no video, and after the video has finished the last second of the audio is repeated.这很简单但很有效,当我在命令提示符下运行
ffplay.exe -i tcp://127.0.0.1:1234?listen -hide_banner
并运行代码以发送视频时,它运行良好,但是当我尝试使用时我接收视频的代码,我得到的只是音频,没有视频,视频结束后,音频的最后一秒会重复。 Heres the recieving code:这是接收代码:
from ffpyplayer.player import MediaPlayer
test = MediaPlayer("tcp://127.0.0.1:1234?listen")
while True:
test.get_frame()
if test == "eof":
break
Thanks for any help and sorry if im just being oblivious to something :P感谢您的帮助和抱歉,如果我只是忘记了某些事情:P
You are only extracting frames from video.mp4 in your code.您只是在代码中从video.mp4中提取帧。
test = MediaPlayer("tcp://127.0.0.1:1234?listen")
while True:
test.get_frame()
if test == "eof":
break
Now, you need to display them using some third-party library since ffpyplayer doesn't provide any inbuilt feature to display frames in a loop.现在,您需要使用一些第三方库来显示它们,因为ffpyplayer不提供任何内置功能来循环显示帧。
Below code uses OpenCV to display extracted frames.下面的代码使用 OpenCV 来显示提取的帧。 Install OpenCV and numpy using below command
使用以下命令安装 OpenCV 和 numpy
pip3 install numpy opencv-python
Change your receiver code to将您的接收器代码更改为
from ffpyplayer.player import MediaPlayer
import numpy as np
import cv2
player = MediaPlayer("tcp://127.0.0.1:1234?listen")
val = ''
while val != 'eof':
frame, val = player.get_frame()
if val != 'eof' and frame is not None:
img, t = frame
w = img.get_size()[0]
h = img.get_size()[1]
arr = np.uint8(np.asarray(list(img.to_bytearray()[0])).reshape(h,w,3)) # h - height of frame, w - width of frame, 3 - number of channels in frame
cv2.imshow('test', arr)
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
you can also run ffplay command directly using python subprocess您也可以使用 python 子进程直接运行 ffplay 命令
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.