如何使用ffpyplayer播放视频流?

huangapple go评论83阅读模式
英文:

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

我正在为自己和朋友做一个有趣的小项目,基本上我想要能够使用ffmpeg来流式传输和接收视频,作为一种屏幕共享应用程序。我完全不懂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 如何使用ffpyplayer播放视频流?

感谢任何帮助,如果我对什么事情视而不见,请原谅 如何使用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. 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)

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

Thanks for any help and sorry if im just being oblivious to something 如何使用ffpyplayer播放视频流?

答案1

得分: 1

你的代码中只是从 video.mp4 中提取帧。

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

现在,你需要使用一些第三方库来显示这些提取出的帧,因为ffpyplayer没有提供内置功能来循环显示帧。

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

pip3 install numpy opencv-python

将你的接收器代码更改为:

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 - 帧的高度, w - 帧的宽度, 3 - 帧中的通道数
        cv2.imshow('test', arr)
        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break

你也可以直接使用Python的subprocess运行ffplay命令。

英文:

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

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.

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

you can also run ffplay command directly using python subprocess

huangapple
  • 本文由 发表于 2020年1月6日 19:13:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611075.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定