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

Can someone help me use fetch to transcode and return a response object that is a stream? #1293

Open
ralyodio opened this issue Aug 5, 2024 · 1 comment

Comments

@ralyodio
Copy link

ralyodio commented Aug 5, 2024

async start(url, user = null, pass = null) {
			console.log('Transcoding:', url);
			const headers = {};

			try {
				if (user && pass) {
					const encodedCredentials = Buffer.from(`${user}:${pass}`).toString('base64');
					headers['Authorization'] = `Basic ${encodedCredentials}`;
				}

				// Fetch the video from the URL
				const response = await fetch(url, { headers, redirect: 'follow' });

				if (!response.ok) {
					throw new Error(`Failed to fetch video: ${response.statusText}`);
				}

				// Create a readable stream from the response
				const videoStream = response.body;
				// const videoStream = response.body;

				// const reader = response.body.getReader();
				// const videoStream = new ReadableStream(response.body);

				// Ensure videoStream is a valid readable stream
				if (!videoStream) {
					throw new Error('Failed to get video stream from response');
				}

				// Create a pass-through stream to handle ffmpeg output
				const outputStream = new PassThrough();

				// outputStream.writable = true;

				// Pipe the video stream through ffmpeg to transcode it to mp4
				ffmpeg(videoStream)
					.format('mp4')
					.output(outputStream)
					.videoCodec('libx264')
					.audioCodec('aac')
					.on('start', (commandLine) => {
						console.log('Spawned Ffmpeg with command: ' + commandLine);
					})
					.on('error', (err) => {
						console.error('Error:', err.message);
						console.error(err);
						outputStream.destroy(err); // destroy the stream in case of error
					})
					.on('progress', (progress) => {
						console.log(JSON.stringify(progress, null, 2));
						console.log('Processing: ' + progress.percent + '% done');
					})
					.on('end', () => {
						console.log('Transcoding finished');
					})
					// .pipe(outputStream)
					.run();

				return outputStream;
			} catch (err) {
				throw err;
			}
		}

I get a conversion error ffmpeg 234

@ralyodio
Copy link
Author

ralyodio commented Aug 5, 2024

I do not want to have to download the entire file first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant