Readable Stream Does Not Emit 'finish' or 'end' Event Causing Code to Not Terminate
- Dominant language
- JavaScript
- Stars
- 4.7k
- Forks
- 852
- PR merge metrics
- No merged PRs in 30d
Description
## Description
I am experiencing an issue with the `ytdl-core` package where the readable stream does not emit the 'finish' or 'end' event, causing the code execution to hang and not terminate as expected.
## Details
I have used the `ytdl-core` for downloading the audio stream from a YouTube video and piping it to a writable stream to save as an MP3 file. However, it seems that the events ('finish' and 'end') that signify the completion of the streaming are not being emitted, leading to an indefinite wait state.
Here's a minimal reproducible example:
```javascript
import fs from "fs";
import ytdl from "ytdl-core";
// Video ID to download
const videoId = "4WEQtgnBu0I";
// Destination file for audio
const outputFilePath = "output/audio.mp3";
// Ensure the directory exists
fs.mkdirSync("output", { recursive: true });
console.log("YTDL Start");
async function downloadAsAudio(ytUrl: string, filePath: string) {
return new Promise(async (resolve) => {
ytdl(ytUrl, {
filter: "audioonly",
quality: "highestaudio",
})
.pipe(fs.createWriteStream(filePath))
.on("finish", () => resolve("Downloaded"));
});
}
await downloadAsAudio(
`https://www.youtube.com/watch?v=${videoId}`,
outputFilePath
);
```
## Expected Behavior
- The stream should emit a 'finish' or 'end' event when the audio download and file write operation is completed.
## Actual Behavior
- The 'finish' or 'end' events are not triggered, resulting in the code hanging and not terminating properly.
## Additional Information
- Node.js version: [Your Node.js version]
- ytdl-core version: [Your ytdl-core version]
- Operating System: [Your OS]
I appreciate your help in resolving this issue.
Thank you!
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by running the minimal ytdl-core example with the reported Node.js and package versions filled in, observing completion and error behavior on the readable and writable streams. Trace the download entry point and stream piping behavior to determine why completion is not observed; done means the audio file finishes writing and the promise resolves without hanging.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, node.js
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100