简体   繁体   English

我将如何使用 ffpyplayer 播放视频流?

[英]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.

相关问题 我将如何 go 关于在 Python 中播放带声音的视频文件并设置播放 position? - How would I go about playing a video file with sound in Python and setting the play position? 我将如何显示总数以显示实际总数? - How would i go about getting the total to show the actual total? 我将如何制作 3 位密码生成器? - How would I go about making a 3 digit pin generator? 我将如何处理多个文件异常? - How would i go about handling multiple file exceptions? 我 go 如何使用列表(元素)function? 你将如何使用这个 function? - How would I go about using the list(element) function? How would you use this function? 使用OpenCV和ffpyplayer实现音视频同步 - Audio and video synchronization with OpenCV and ffpyplayer 我将如何在类似程序的命令行上进行参数设置? - How would I go about making an argument on my command-line like program? 我将如何在实时场景中使用 concurrent.futures 和队列? - How would I go about using concurrent.futures and queues for a real-time scenario? 我将如何重新编写文件,使其变得更易于管理? - How would I go about re-writing a file so that it may become more manageable? 我将如何使用 x 和 y 坐标在我的 pygame 游戏中移动一块 - how would I go about moving a piece in my pygame game using the x and y coordinates
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM