admin管理员组文章数量:1279175
I'm using yt-dlp in a Node.js Express application to download videos from YouTube. I'm extracting the video title and using it to name the downloaded file, but all downloaded files are still named video.mp4 instead of the extracted title.
Here's the relevant code from my Express route:
app.get('/download', async (req, res) => {
const videoURL = req.query.url;
const format = req.query.format || 'mp4';
if (!videoURL) {
return res.status(400).send({ error: 'No URL provided' });
}
const cookiesFile = path.resolve(__dirname, 'cookies.txt');
// Validate cookies file exists
if (!fs.existsSync(cookiesFile)) {
console.error('Missing cookies.txt');
return res.status(500).send({ error: 'Authentication failed' });
}
// Set timeout to handle long downloads
req.setTimeout(600000); // 10 minutes
// Get video metadata to extract the title
exec(`yt-dlp --cookies "${cookiesFile}" --get-title ${videoURL}`, (error, stdout, stderr) => {
if (error) {
console.error('Failed to get video title:', stderr);
return res.status(500).send({ error: 'Failed to get video title' });
}
const videoTitle = stdout.trim().replace(/[<>:"/\\|?*]+/g, ''); // Sanitize title
const outputFile = path.resolve(os.tmpdir(), `${videoTitle}.${format}`);
// Configure yt-dlp command
const command = format === 'mp3'
? `yt-dlp --cookies "${cookiesFile}" --extract-audio --audio-format mp3 -o "${outputFile}" ${videoURL}`
: `yt-dlp --cookies "${cookiesFile}" -o "${outputFile}" --merge-output-format mp4 ${videoURL}`;
exec(command, async (error, stdout, stderr) => {
try {
if (error) {
throw new Error(`yt-dlp error: ${stderr}`);
}
// Verify output file exists
if (!fs.existsSync(outputFile)) {
throw new Error('Output file not created');
}
// Validate file size
const stats = fs.statSync(outputFile);
const maxSize = 1024 * 1024 * 500; // 500MB
if (stats.size > maxSize) {
fs.unlinkSync(outputFile);
return res.status(413).send({ error: 'File exceeds maximum size limit' });
}
// Set proper content headers
res.setHeader('Content-Type', format === 'mp3' ? 'audio/mpeg' : 'video/mp4');
res.setHeader('Content-Length', stats.size);
res.setHeader('Content-Disposition', `attachment; filename="${path.basename(outputFile)}"`);
// Stream the file
const fileStream = fs.createReadStream(outputFile);
fileStream.pipe(res);
// Cleanup after stream finishes
fileStream.on('end', () => {
fs.unlink(outputFile, (err) => {
if (err) console.error('Cleanup error:', err);
});
});
} catch (err) {
console.error('Download failed:', err);
// Cleanup failed download
if (fs.existsSync(outputFile)) {
fs.unlinkSync(outputFile);
}
res.status(500).send({
error: 'Download failed',
details: err.message
});
}
});
});
});
The issue is that despite extracting the video title and using it in the outputFile path, the downloaded file is consistently named video.mp4. I've verified that the videoTitle variable contains the correct title after sanitization, and the outputFile path seems to be constructed correctly.
What I've Tried:
I've checked the output of the yt-dlp --get-title command and confirmed that it's returning the expected title. I've added console.log(outputFile) to verify that the file path is being generated with the correct title. I've checked that the format variable is correctly set to either mp4 or mp3 based on the query parameter. I have tried to change the output file path to a static path, and the file is still named video.mp4.
本文标签: javascriptName of video still quotvideomp4quot after downloading using ytdlpStack Overflow
版权声明:本文标题:javascript - Name of video still "video.mp4" after downloading using yt-dlp - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741223997a2361514.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论