Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FFMPEG exited with code 8. Youtube Error 403 #1

Closed
PoProstuWitold opened this issue Jan 22, 2025 · 1 comment
Closed

FFMPEG exited with code 8. Youtube Error 403 #1

PoProstuWitold opened this issue Jan 22, 2025 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@PoProstuWitold
Copy link
Owner

Workaround for YouTube API Issues

Due to recent changes in the YouTube API, Sayuna is currently unable to load music from YouTube correctly because ffmpeg crashes with error code 8: YouTube throws 403 thus making streaming music not possible.

While there is no official fix yet, a temporary workaround has been identified that partially resolves the issue. This workaround is detailed in the following pull request from the distubejs/ytdl-core repository.

Why isn't this included in Sayuna's repository and Docker image?

This workaround is considered a temporary and suboptimal solution with known limitations, such as frequent 403 errors causing songs to be cut off prematurely. Due to these issues, it has not been integrated into the official Sayuna repository or Docker image. The necessary fixes will be implemented once they are officially merged into the upstream branch of the distubejs/ytdl-core repository.

Steps to Apply the Workaround

1. Create the Patch Utility

Create file utils/ytdlPatch.ts with this contents:

import fs from 'fs'
import fsPromise from 'fs/promises'
import { Readable } from 'stream'
import { finished } from 'stream/promises'

export const ytdlPatch = async () => {
  
	//@ts-ignore
	const patchFile = async (file, patchUrl) => {
		const stream = fs.createWriteStream(file)
		const { body } = await fetch(patchUrl)
		//@ts-ignore
		return finished(Readable.fromWeb(body).pipe(stream))
	}
  
	//@ts-ignore
	const replaceInFile = (file, find, replace) => 
		fsPromise.readFile(file)
			//@ts-ignore
			.then(data => data.toString().replace(find, replace))
			//@ts-ignore
			.then(data => fsPromise.writeFile(file, data))
  
	// https://github.com/distubejs/ytdl-core/pull/163
	const patchBaseUrl = 'https://raw.githubusercontent.com/ToddyTheNoobDud/ytdl-core-stuff/37acb1c7ca203bb33c1219bffe5fb2b820b52f69/'
	const buggedInfo = './node_modules/@distube/ytdl-core/lib/info.js'
	const buggedUtils = './node_modules/@distube/ytdl-core/lib/utils.js'
	const buggedYoutubeIndex = './node_modules/@distube/youtube/dist/index.js'
	await patchFile(buggedInfo, patchBaseUrl + 'lib/info.js')
	await patchFile(buggedUtils, patchBaseUrl + 'lib/utils.js')
  
	// https://github.com/distubejs/ytdl-core/issues/118#issuecomment-2441791441
	// I think you can also edit YoutubePlugin.ytdlOptions,
	// but I like to keep this in a single function
	// (https://discord.com/channels/732254550689316914/1272308260061319269/1330693171172343840)
	await replaceInFile(
		buggedUtils,
		`["WEB", "WEB_CREATOR", "IOS", "WEBEMBEDDED", "MWEB"]`,
		`["WEB", "WEB_CREATOR", "IOS", "WEBEMBEDDED", "MWEB", "ANDROID"]`
	)
  
	// https://github.com/distubejs/ytdl-core/issues/84#issuecomment-2295590460
	// https://discord.com/channels/732254550689316914/1271765204253933609/1275273097120841781
	await replaceInFile(
		buggedYoutubeIndex,
		`const info = await import_ytdl_core.default.getInfo(song.url, this.ytdlOptions);`,
		`let info = await import_ytdl_core.default.getInfo(song.url, this.ytdlOptions);
		info.formats = info.formats.filter((f) => f.hasAudio); /* 403 Error Workaround */`
	)
	
	console.log(`Applied hotfix for DisTubeError "ffmpeg exited with code 1" or 403 Errors`)
}

2. Modify main.ts

Update code in main.ts:

import { ytdlPatch } from './utils/ytdlPatch.js'

// ...


        public async bot(token: string) {
		try {
			await importx(
				`${dirname(import.meta.url)}/{events,commands,api}/**/*.{ts,js}`
			)
	
			await ytdlPatch() // this line
			await this.client.login(token)
			this.logger.info('Bot has logged in...')
		} catch (err) {
			console.log(err)
			throw err
		}
	}
@PoProstuWitold PoProstuWitold added the bug Something isn't working label Jan 22, 2025
@PoProstuWitold PoProstuWitold self-assigned this Jan 22, 2025
@PoProstuWitold
Copy link
Owner Author

Fixed with Sayuna v2.3.1 release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant