【问题标题】:(Ffmpeg) How to play live audio in the browser from received UDP packets using Ffmpeg?(Ffmpeg) 如何使用 Ffmpeg 从接收到的 UDP 数据包在浏览器中播放实时音频?
【发布时间】:2023-03-14 23:50:02
【问题描述】:

我有充当 UDP 服务器和 UDP 客户端的 .NET Core 控制台应用程序

  • UDP 客户端通过接收音频数据包。
  • UDP 服务器,通过发送每个接收到的数据包。

这是控制台应用程序的示例代码:

static UdpClient udpListener = new UdpClient();
    static IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("192.168.1.230"), 6980);
    static IAudioSender audioSender = new UdpAudioSender(new IPEndPoint(IPAddress.Parse("192.168.1.230"), 65535));

    static void Main(string[] args)
    {
        udpListener.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
        udpListener.Client.Bind(endPoint);

        try
        {
            udpListener.BeginReceive(new AsyncCallback(recv), null);
        }
        catch (Exception e)
        {
            throw e;
        }

        Console.WriteLine("Press enter to dispose the running service");
        Console.ReadLine();
    }

    private async static void recv(IAsyncResult res)
    {
        byte[] received = udpListener.EndReceive(res, ref endPoint);
        OnAudioCaptured(received);
        udpListener.BeginReceive(new AsyncCallback(recv), null);
    }

另一方面,我有一个 node js API 应用程序,它假设将 FFmpeg 命令作为子进程执行并执行以下操作

  • 从控制台应用 UDP 服务器接收音频数据包作为输入。
  • 将接收到的字节转换为WebM
  • 将结果输出到响应中。

最后,在客户端,我应该有一个源值等于http://localhost:3000的音频元素

目前,我只能执行这个 FFmpeg 命令:

ffmpeg -f  s16le  -ar 48000 -ac 2 -i 'udp://192.168.1.230:65535' output.wav

执行以下操作

  • 接收 UDP 数据包作为输入
  • 将接收到的字节转换为 output.wav 音频文件。

如何在节点 js 服务器中执行一个子进程,该服务器接收 UDP 数据包并将结果作为 Webm 输出到响应中?

【问题讨论】:

  • 最简单的方法是使用 FFmpeg 之类的东西通过子进程将这些 PCM 样本包装在 WebM 中。 ffmpeg -i - -acodec copy -f webm -(或者,如果您不需要无损音频,请删除 -acodec copy。)然后,将该流输出到您的客户端。在客户端,您只需要执行<audio src="https://your-node-server.example.com/stream" controls preload="none"></audio> 之类的操作。简单得多。无需 Socket.IO,浏览器处理缓冲、播放、采样率转换和通道映射。
  • @Brad 既然 node js 不支持 AudioContext,我怎么能做到这一点?
  • 它不需要,而且无论如何这对你没有帮助。将 FFmpeg 作为子进程执行。将您的音频数据通过管道传输到它,将 WebM 数据返回,然后将其传输到客户端。
  • 试试这样的:ffmpeg -f s16le -ar 48000 -ac 2 -i 'udp://192.168.1.230:65535' -b:a 128k -f webm -。使用child_process.spawn() (nodejs.org/api/…)。将生成的子进程对象的stdout 流和.pipe() 带到res。 (确保首先设置Content-Type: audio/webm 标头。)
  • 浏览器的开发者工具怎么说?您如何加载音频客户端?您可能还需要在 Content-Type 响应标头中指定编解码器。

标签: node.js c#-4.0 ffmpeg audio-streaming live-streaming


【解决方案1】:

非常感谢 Brad,他指导我找到了解决方案。

在节点 js 服务器中,我必须执行以下操作

  • 将 Ffmpe 作为接收 UDP 数据包作为输入的子进程执行
  • 将子进程的每个结果输出到响应中。

这是node js服务器的源代码:

var http = require("http");
var port = 8888;
var host = "localhost";
var children = require("child_process");

http
  .createServer(function (req, res) {
    //ffmpeg -f s16le -ar 48000 -ac 2 -i 'udp://192.168.1.230:65535' -b:a 128k -f webm -
    var ffm = children.spawn(
      "ffmpeg",
      "-f s16le -ar 48000 -ac 2 -i udp://192.168.1.230:65535 -b:a 128k -f webm -".split(
        " "
      )
    );

    res.writeHead(200, { "Content-Type": "audio/webm;codecs=vorbis" });
    ffm.stdout.on("data", (data) => {
      console.log(data);
      res.write(data);
    });
  })
  .listen(port, host);

console.log("Server running at http://" + host + ":" + port + "/");

客户端:

  <audio src="http://loclahost:8888" type='audio/webm; codecs="vorbis"' controls preload="none"></audio>

【讨论】:

    猜你喜欢
    • 2020-09-23
    • 2017-07-12
    • 2013-11-21
    • 2015-08-24
    • 1970-01-01
    • 2011-06-27
    • 2015-12-28
    • 2016-05-13
    • 2020-09-13
    相关资源
    最近更新 更多