You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何通过编程在Discord上流式传输视频?能否程序化调用Go Live功能?

Hey there! Let's break down your two Discord streaming questions clearly—I've tinkered with these APIs a fair bit, so I can share what I know from hands-on experience.

1. 编程实现Discord视频流式传输

Absolutely, you can pull off video streaming via Discord's official APIs using libraries like discord.js (JavaScript/TypeScript) or discord.py (Python). Here's the lowdown on how to make it work:

Core Requirements & Steps

  • Permissions: Your bot needs CONNECT and SPEAK permissions for the target voice channel, and the user triggering the stream should have video access enabled in that channel.
  • Video Encoding Rules: Discord only accepts H.264-encoded video with specific parameters: max 30fps, resolutions up to 1080p, yuv420p pixel format, and bitrates adjusted to match your resolution (e.g., 2Mbps for 720p, 4Mbps for 1080p). You'll almost always need ffmpeg to transcode your source (local file, camera feed, screen capture) into this compatible format.
  • Library Integration: Official voice libraries (like @discordjs/voice for Node.js) handle the heavy lifting of sending the transcoded stream to Discord's servers.

Quick Working Example (Node.js + discord.js)

Here's a snippet that streams a local video file to a voice channel using ffmpeg for transcoding:

const { Client, GatewayIntentBits } = require('discord.js');
const { joinVoiceChannel, createAudioPlayer, createAudioResource } = require('@discordjs/voice');
const ffmpeg = require('ffmpeg-static');
const { spawn } = require('child_process');

const client = new Client({
  intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates]
});

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on('messageCreate', async (message) => {
  if (message.content === '!start-stream') {
    const voiceChannel = message.member.voice.channel;
    if (!voiceChannel) return message.reply('Hop into a voice channel first!');

    // Join the target voice channel
    const connection = joinVoiceChannel({
      channelId: voiceChannel.id,
      guildId: voiceChannel.guild.id,
      adapterCreator: voiceChannel.guild.voiceAdapterCreator,
    });

    // Use ffmpeg to transcode the video to Discord's required format
    const ffmpegProcess = spawn(ffmpeg, [
      '-i', './my-video.mp4', // Replace with your video file path
      '-c:v', 'libx264',
      '-preset', 'ultrafast', // Low-latency preset for real-time streaming
      '-tune', 'zerolatency',
      '-r', '30', // Lock to 30fps
      '-b:v', '2M', // 2Mbps bitrate (adjust for higher/lower resolutions)
      '-f', 'rawvideo',
      '-pix_fmt', 'yuv420p',
      '-' // Pipe output to stdout for the stream
    ]);

    // Create a video resource and play it through the voice connection
    const videoResource = createAudioResource(ffmpegProcess.stdout, { inputType: 'video' });
    const player = createAudioPlayer();
    
    player.play(videoResource);
    connection.subscribe(player);

    message.reply('Video stream started in your voice channel!');
  }
});

client.login('YOUR_BOT_TOKEN');

Pro Tips

  • For camera/screen capture, replace the local file path with a capture device (e.g., /dev/video0 on Linux) or use a library like screen-recorder to grab your desktop feed.
  • Adjust the bitrate based on your server's boost level—Discord allows higher bitrates for boosted servers to avoid pixelation.
2. 能否通过编程调用Discord的「Go Live」功能直播

Short and clear: No, not via Discord's official API.

Go Live is a client-exclusive feature built for manual user-initiated screen/window sharing, and Discord has never exposed any endpoints or methods in their public API to trigger it programmatically.

While some developers have reverse-engineered Discord's internal client APIs to simulate Go Live, this directly violates Discord's Terms of Service (specifically the section banning unauthorized tools or API usage). Doing this could get your bot or personal account permanently banned.

If you want a public stream visible in a server's "Live" tab, your only official option is the voice channel video streaming method above—but note that this stream will only be visible to users in the voice channel, not listed as a Go Live stream.


内容的提问来源于stack exchange,提问作者keanu_reeves

火山引擎 最新活动