【问题标题】:Combine Audio and Images in Stream在流中合并音频和图像
【发布时间】:2017-12-12 13:30:50
【问题描述】:

我希望能够动态创建图像,也可以动态创建音频,并且能够将它们组合成一个 rtmp 流(用于 Twitch 或 YouTube)。目标是在 Python 3 中实现这一点,因为这是我的机器人编写的语言。不必保存到磁盘的奖励积分。

到目前为止,我已经弄清楚了如何使用 ffmpeg 流式传输到 rtmp 服务器,方法是加载 PNG 图像并循环播放,以及加载 mp3,然后在流中将它们组合在一起。问题是我必须从文件中至少加载其中一个。

我知道我可以使用 Moviepy 创建视频,但我不知道是否可以将视频从 Moviepy 流式传输到 ffmpeg 或直接传输到 rtmp。我认为我必须生成很多非常短的剪辑并发送它们,但我想知道是否有现成的解决方案。

我听说还有 OpenCV 可以流式传输到 rtmp,但不能处理音频。

我已经成功测试过的 ffmpeg 命令的编辑版本是

ffmpeg -loop 1 -framerate 15 -i ScreenRover.png -i "Song-Stereo.mp3" -c:v libx264 -preset fast -pix_fmt yuv420p -threads 0 -f flv rtmp://SITE-SUCH-AS-TWITCH/.../STREAM-KEY

cat Song-Stereo.mp3 | ffmpeg -loop 1 -framerate 15 -i ScreenRover.png -i - -c:v libx264 -preset fast -pix_fmt yuv420p -threads 0 -f flv rtmp://SITE-SUCH-AS-TWITCH/.../STREAM-KEY

我知道这些命令没有正确设置以实现流畅的流式传输,结果导致 Twitch 和 Youtube 的播放器都搞砸了,我必须想办法解决这个问题。

这个问题是我认为我不能在现场创建图像和音频时同时流式传输它们。我必须从硬盘驱动器加载其中之一。当试图对命令或用户聊天或其他任何需要实时反应的事情做出反应时,这会成为一个问题。我也不想通过不断保存来破坏我的硬盘。

至于python代码,到目前为止我为了创建视频而尝试过的是以下代码。这仍然保存到高清并且没有实时响应,所以这对我来说不是很有用。视频本身没问题,除了一个例外,随着时间的推移,二维码显示的时钟与视频的时钟之间的距离随着视频接近尾声而越来越远。如果它在直播时出现,我可以解决这个限制。

def make_frame(t):
  img = qrcode.make("Hello! The second is %s!" % t)
  return numpy.array(img.convert("RGB"))

clip = mpy.VideoClip(make_frame, duration=120)
clip.write_gif("test.gif",fps=15)

gifclip = mpy.VideoFileClip("test.gif")
gifclip.set_duration(120).write_videofile("test.mp4",fps=15)

我的目标是能够按照以下的伪代码生成一些东西

original_video = qrcode_generator("I don't know, a clock, pyotp, today's news sources, just anything that can be generated on the fly!")
original_video.overlay_text(0,0,"This is some sample text, the left two are coordinates, the right three are font, size, and color", Times_New_Roman, 12, Blue)
original_video.add_audio(sine_wave_generator(0,180,2)) # frequency min-max, seconds

# NOTICE - I did not add any time measurements to the actual video itself. The whole point is this is a live stream and not a video clip, so the time frame would be now. The 2 seconds list above is for our psuedo sine wave generator to know how long the audio clip should be, not for the actual streaming library.

stream.send_to_rtmp_server(original_video) # Doesn't matter if ffmpeg or some native library

上面的示例是我在 Python 中创建视频然后流式传输方面所寻找的。我不是想创建一个剪辑然后再流式传输它,我试图让程序能够响应外部事件,然后更新它的流以做它想做的任何事情。它有点像聊天机器人,但使用的是视频而不是文本。

def track_movement(...):
  ...
  return ...

original_video = user_submitted_clip(chat.lastVideoMessage)
original_video.overlay_text(0,0,"The robot watches the user's movements and puts a blue square around it.", Times_New_Roman, 12, Blue)
original_video.add_audio(sine_wave_generator(0,180,2)) # frequency min-max, seconds

# It would be awesome if I could also figure out how to perform advance actions such as tracking movements or pulling a face out of a clip and then applying effects to it on the fly. I know OpenCV can track movements and I hear that it can work with streams, but I cannot figure out how that works. Any help would be appreciated! Thanks!

因为我忘了添加导入,所以我的文件中有一些有用的导入!

import pyotp
import qrcode
from io import BytesIO
from moviepy import editor as mpy

pyotp 库用于生成一次性验证码,qrcode 用于 qr 码,BytesIO 用于虚拟文件,moviepy 用于生成 GIF 和 MP4。我相信 BytesIO 可能对于将数据管道传输到流服务很有用,但是如何发生,完全取决于数据如何发送到服务,无论是命令行上的 ffmpeg(来自子进程导入 Popen、PIPE)还是本机图书馆。

【问题讨论】:

标签: python-3.x ffmpeg rtmp moviepy


【解决方案1】:

您是否正在使用 ffmpeg.exe 并通过 CMD 运行命令?如果是这样,您可以使用 concat demuxer 或管道。当您使用 concat demuxer 时,ffmpeg 可以从文本文件中获取图像输入。文本文件应该包含图像路径,ffmpeg 可以从不同的文件夹中找到这些图像。以下代码行显示了如何使用 concat demuxer。图像位置保存到 input.txt 文件中。

ffmpeg -f concat -i input.txt -vsync vfr -pix_fmt yuv420p output.mp4

但最合适的解决方案是使用数据管道将图像提供给 ffmpeg。

cat *.png | ffmpeg -f image2pipe -i - output.mkv

您可以查看this 链接以查看有关 ffmpeg 数据管道的更多信息。

实时生成多个视频和流式传输并不是一个非常稳定的解决方案。您可能会遇到几个问题。

【讨论】:

    【解决方案2】:

    我已决定使用 Gstreamer 即时创建我的流。它可以让我获取单独的视频和音频流并将它们组合在一起。我现在还没有确切的工作示例,但我希望很快就会有答案或自己弄清楚,Gstreamer in Python exits instantly, but is fine on command line

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-27
      • 1970-01-01
      • 1970-01-01
      • 2020-11-01
      • 1970-01-01
      • 2019-12-01
      相关资源
      最近更新 更多