本文介绍了我将如何使用 ffpyplayer 播放视频流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次在这里发布海报,所以放轻松.

First time poster here, so go easy on me.

我正在为自己和朋友们做一个有趣的小项目,基本上我希望能够使用 ffmpeg 流式传输和接收视频,作为一种屏幕共享应用程序.我是一个完整的 python 菜鸟,我只是离开了每个人的文档.这是我要发送的内容:

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. I'm a complete python noob and im just going off of the documentation for each.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)

这很简单但很有效,当我在命令提示符下运行 ffplay.exe -i tcp://127.0.0.1:1234?listen -hide_banner 并运行代码以发送视频时,它工作得很好,但是当我尝试使用我的代码接收视频时,我得到的只是音频,没有视频,并且在视频完成后,音频的最后一秒会重复.这是接收代码:

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.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

感谢您的帮助,如果我只是忘记了某些事情,我很抱歉:P

Thanks for any help and sorry if im just being oblivious to something :P

推荐答案

您只是在代码中从 video.mp4 中提取帧.

You are only extracting frames from video.mp4 in your code.

test = MediaPlayer("tcp://127.0.0.1:1234?listen")
while True:
    test.get_frame()
    if test == "eof":
        break

现在,您需要使用一些第三方库来显示它们,因为 ffpyplayer 没有提供任何内置功能以循环显示帧.

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.

下面的代码使用 OpenCV 来显示提取的帧.使用以下命令安装 OpenCV 和 numpy

Below code uses OpenCV to display extracted frames. Install OpenCV and numpy using below command

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

您也可以直接使用 python subprocess

you can also run ffplay command directly using python subprocess

这篇关于我将如何使用 ffpyplayer 播放视频流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 14:56