dank074 / Discord-video-stream

Experiment for making video streaming work for discord selfbots.
174 stars 33 forks source link

Cannot play ytdl-core stream: "cannot play video Input stream error: aborted" #35

Open MiguelEXE opened 11 months ago

MiguelEXE commented 11 months ago

I'm trying to do a bot that screenshares a youtube video using ytdl-core library but in the middle of the video it crashes with this reason:

node:internal/process/promises:288
            triggerUncaughtException(err, true /* fromPromise */);
            ^

[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "cannot play video Input stream error: aborted".] {
  code: 'ERR_UNHANDLED_REJECTION'
}

Source code:

require("dotenv").config();
const Discord = require("discord.js-selfbot-v13");
const {Streamer, streamLivestreamVideo, setStreamOpts} = require("@dank074/discord-video-stream");
const ytdl = require("ytdl-core");
const streamer = new Streamer(new Discord.Client());

// probally best settings for a 1920x1080 60fps video.
setStreamOpts({
    video_codec: "H264",
    fps: 60,
    hardware_acceleration: true,
    width: 1920,
    height: 1080,
    bitrateKbps: 6000,
    maxBitrateKbps: 10000
});

const PREFIX = process.env.PREFIX;
const CONTROLLER_ID = process.env.CONTROLLER_ID;
streamer.client.once("ready", () => {
    console.log(`Done loading. prefix='${PREFIX}'`);
});
async function playStream(guild_id, voice_id, stream){
    await streamer.joinVoice(guild_id, voice_id);
    const udp = await streamer.createStream();
    udp.mediaConnection.setSpeaking(true);
    udp.mediaConnection.setVideoStatus(true);
    await streamLivestreamVideo(stream, udp, true);
    udp.mediaConnection.setSpeaking(false);
    udp.mediaConnection.setVideoStatus(false);
    streamer.stopStream();
}
streamer.client.on("messageCreate", async message => {
    if((message.author.id != CONTROLLER_ID) || (!message.content.startsWith(PREFIX))) return;
    const args = message.content.slice(PREFIX.length).split(/ +/);
    const cmd = args.shift();
    if(cmd === "ytplay"){
        if(!message.member.voice){
            return message.reply("Be connected in a voice channel!");
        }
        const youtubeLink = args.shift();
        if(!youtubeLink){
            return message.reply("Specify a youtube link!");
        }
        const youtubeStream = ytdl(youtubeLink, {
            format: "medium"
        });
        await playStream(message.guild.id, message.member.voice.channelId, youtubeStream);
    }
});

streamer.client.login(process.env.TOKEN);
dank074 commented 10 months ago

If you have hardware acceleration enabled can you verify that your environment supports it

Or it could be related to this: https://github.com/fent/node-ytdl-core/issues/902

Suggested solution was to increase highWatermark for ytdl