FFMPEG等待关闭管道以开始处理数据

发布于 2025-02-08 06:54:26 字数 1267 浏览 3 评论 0原文

我在这个问题上挣扎。 在这里,我试图使用来自图像和音频文件的FFMPEG流式传输视频,此代码在下面工作正常,但是在stdin关闭后它开始流式传输,我希望FFMPEG在接收到的管道输入时开始处理该管道输入,并立即输出结果。

p = subprocess.Popen(
    [
        'ffmpeg',
        '-y',
        '-re',
        '-f', 'image2pipe',
        '-vcodec', 'mjpeg',
        '-framerate', '15',
        '-analyzeduration', '2147483647',
        '-probesize', '2147483647',
        '-i', '-',
        '-vn', '-i', audio_path,
        '-c:v', 'libx264',
        '-pix_fmt', 'yuv420p',
        '-preset', 'ultrafast',
        '-c:a', 'aac',
        '-f', 'flv',
        rtmp_url
    ],
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE
)

time.sleep(2)

i = 0
while True:
    current_images = [name for name in os.listdir(img_dir) if os.path.isfile(os.path.join(img_dir, name))]
    current_images = sorted(current_images)
    if i > fnum-1:
        print("frame read failed")
        break
    try:
        img = cv2.imread(os.path.join(img_dir, current_images[i]))
        success, buffer = cv2.imencode('.jpg', img)
        frame = buffer.tobytes()
        p.stdin.write(frame)
        i += 1
    except:
        time.sleep(2)
        pass

p.stdin.close()
p.wait()

为什么FFMPEG等待关闭管道开始处理?可以配置为启动接收流的实时转码吗?

您知道如何说服FFMPEG立即开始产生输出吗?

谢谢你!

I'm struggling with this issue.
Here I am trying to stream video using ffmpeg from images and audio file this code below works fine but it is start streaming after stdin closed, I would expect ffmpeg to start processing the pipe input as it is received, and immediately output the result.

p = subprocess.Popen(
    [
        'ffmpeg',
        '-y',
        '-re',
        '-f', 'image2pipe',
        '-vcodec', 'mjpeg',
        '-framerate', '15',
        '-analyzeduration', '2147483647',
        '-probesize', '2147483647',
        '-i', '-',
        '-vn', '-i', audio_path,
        '-c:v', 'libx264',
        '-pix_fmt', 'yuv420p',
        '-preset', 'ultrafast',
        '-c:a', 'aac',
        '-f', 'flv',
        rtmp_url
    ],
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE
)

time.sleep(2)

i = 0
while True:
    current_images = [name for name in os.listdir(img_dir) if os.path.isfile(os.path.join(img_dir, name))]
    current_images = sorted(current_images)
    if i > fnum-1:
        print("frame read failed")
        break
    try:
        img = cv2.imread(os.path.join(img_dir, current_images[i]))
        success, buffer = cv2.imencode('.jpg', img)
        frame = buffer.tobytes()
        p.stdin.write(frame)
        i += 1
    except:
        time.sleep(2)
        pass

p.stdin.close()
p.wait()

Why is ffmpeg waiting to close the pipe to start processing? Can it be configured to start a live transcoding of the received stream?

Do you know how can I convince ffmpeg to start producing output immediately?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文