JavaScript

Twitch API 오류

ki

kimrasng
답변 완료
30 XP

Twitch API로 특정 유저의 방송이 올라인이되면 녹화를 시작하고 방송이 종료되면 녹화파일을 저장하고 방송이 켜질때까지 대기하는 프로그램을 만들었는데 개속해서 API쪽에 오류가 뜹니다..

const tmi = require('tmi.js');
const ffmpeg = require('fluent-ffmpeg');
const fs = require('fs');
const path = require('path');

const twitchChannel = 'user_id';

const client = new tmi.Client({
  channels: [twitchChannel],
});

let isBroadcasting = false;

const ffmpegCommand = ffmpeg();

const currentDate = new Date();
const formattedDate = currentDate.toISOString().replace(/:/g, '-').split('.')[0]; // 파일명에 사용할 형식으로 포맷

const outputPath = 'C:\\twitch\\gosegugosegu';
const outputFileName = path.join(outputPath, `output_${formattedDate}.mp4`);

client.connect();

client.on('message', (channel, tags, message, self) => {
});

async function checkBroadcastStatus() {
  try {
    const response = await client.api.helix.streams.getStreamByUserName(twitchChannel);

    if (response && response.data && response.data.length > 0) {
      const streamInfo = response.data[0];
      const isLive = streamInfo.type === 'live';

      if (isLive !== isBroadcasting) {
        isBroadcasting = isLive;

        if (isBroadcasting) {
          console.log('방송이 시작되었습니다.');
          ffmpegCommand.input(`https://www.twitch.tv/${twitchChannel}`)
            .inputFormat('lavfi')
            .inputOptions('-re')
            .videoCodec('copy')
            .audioCodec('aac')
            .output(outputFileName)
            .on('end', () => {
              console.log(`녹화가 완료되었습니다. 파일은 ${outputFileName}에 저장되었습니다.`);
              process.exit();
            })
            .run();
        } else {
          console.log('방송이 종료되었습니다. 대기 중...');
        }
      }
    } else {
      console.log('스트림 정보를 가져올 수 없습니다. 대기 중...');
    }
  } catch (error) {
    console.error('방송 상태 확인 중 오류:', error);
  }
}

setInterval(checkBroadcastStatus, 4000);

process.on('SIGINT', () => {
  console.log('프로그램이 종료되었습니다.');
  ffmpegCommand.kill();
  process.exit();
});


불러오는 중...