FFMPEG node.js 与 ytdl 一起使用

问题描述 投票:0回答:1

我有nodejs javascript代码,我希望下载视频和音频,以便将其与ffmpeg结合起来。首先,视频比应有的长度长,我不知道为什么。其次,ffmpeg 无法工作,因为我收到错误,ffmpeg 无法找到(但我通过 npm 正确安装了 Fluent-ffmpeg)

我的代码:

const fs = require('fs');
const ytdl = require('ytdl-core');
const ffmpeg = require('fluent-ffmpeg');



// Function to download the youtube video
function download() {
    const linkInput = document.getElementById('linkInp');
    const resolution = document.getElementById('resolution');
    const videoTitle = document.getElementById('videoTitle');
    const videoAuthor = document.getElementById('videoAuthor');
    const videoUploadDate = document.getElementById('videoUploadDate');
    const videoPicture = document.getElementById('videoPic');
    const link = linkInput.value;
    console.log('Downloading following video from link: ', link, ' with resolution id: ', resolution.value);

    ytdl.getInfo(link).then((info) => {
        const format = ytdl.chooseFormat(info.formats, { quality: `${resolution.value}` });
        const videoFilePath = `${info.videoDetails.title}.${format.container}`;
        const audioFilePath = `${info.videoDetails.title}.mp3`;

        // before download, collect meta datas
        videoTitle.innerHTML = info.videoDetails.title;
        videoAuthor.innerHTML = info.videoDetails.author.name;
        videoUploadDate.innerHTML = info.videoDetails.uploadDate;
        videoPicture.src = info.videoDetails.thumbnails[0].url;

        // start downloading the video
        ytdl.downloadFromInfo(info, { format: format }).pipe(fs.createWriteStream(videoFilePath));

        // download audio separately
        ytdl.downloadFromInfo(info, { filter: 'audioonly' }).pipe(fs.createWriteStream(audioFilePath)).on('finish', () => {
            // merge video and audio after both are downloaded
            ffmpeg()
                .input(videoFilePath)
                .input(audioFilePath)
                .videoCodec('copy')
                .audioCodec('copy')
                .save(`${info.videoDetails.title}_with_audio.${format.container}`)
                .on('end', () => {
                    console.log('Video with audio merged successfully!');
                    fs.unlink(videoFilePath, (err) => {
                        if (err) console.error(err);
                    });
                    fs.unlink(audioFilePath, (err) => {
                        if (err) console.error(err);
                    });
                });
        });

    }).catch((err) => {
        console.error(err);
        document.getElementById('msg').style.display = 'block';
        document.getElementById('message').innerHTML = err;
    });
}

感谢您的每一次帮助!

javascript node.js ffmpeg
1个回答
0
投票

您收到的错误是因为包

fluent-ffmpeg
仅提供了一个简单的接口来运行 ffmpeg 命令。要使用它,您必须在部署代码的计算机中手动安装 ffmpeg。

有一个库

ffmpeg-static
,它提供了 ffmpeg 的静态构建,因此您无需担心在机器中手动安装 ffmpeg。

const ffmpegStatic = require('ffmpeg-static');
const ffmpeg = require('fluent-ffmpeg');

ffmpeg.setFfmpegPath(ffmpegStatic);

// Now you can use ffmpeg in your Node.js project
© www.soinside.com 2019 - 2024. All rights reserved.