(Ffmpeg)如何使用Ffmpeg在收到的UDP数据包中在浏览器中播放实时音频? [英] (Ffmpeg) How to play live audio in the browser from received UDP packets using Ffmpeg?

查看:796
本文介绍了(Ffmpeg)如何使用Ffmpeg在收到的UDP数据包中在浏览器中播放实时音频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have .NET Core console application which acts as UDP Server and UDP Client

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

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

Here's a sample code of the console app:

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);
    }

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

On the other side, I have a node js API application, which supposes to execute an FFmpeg command as a child process and to do the following

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

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

Finally, in the client-side, I should have an audio element with source value equals to the http://localhost:3000

目前,我只能执行以下FFmpeg命令:

For now, I can only execute this FFmpeg command:

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

执行以下操作

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

我将如何在接收UDP数据包的节点js服务器中执行子进程,并将结果以Webm的形式发送到响应中?

How would I execute a child process in the node js server which receives the UDP packets and pipe out the result into the response as Webm?

推荐答案

非常感谢Brad,他将我引向了解决方案.

Huge thanks to Brad, he directed me to the solution.

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

In node js server I had to do the following

  • 将Ffmpe作为子进程执行,该进程接收UDP数据包作为输入
  • 将子流程的每个结果放入响应中.

这是节点js服务器的源代码:

Here's the source code the node js server:

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>

这篇关于(Ffmpeg)如何使用Ffmpeg在收到的UDP数据包中在浏览器中播放实时音频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆