![](/img/trans.png)
[英]Ffmpeg send duration of video to client (using node-fluent-ffmpeg)
[英]How to add background music to a video file on a specific video second? In node-fluent-ffmpeg
我有一個 12 秒長的 audio.mp3 文件,而 video.mp4 是 60 秒長。
我需要在視頻的第 40 秒插入 audio.mp3。
如何使用 node-fluent-ffmpeg 做到這一點?
我住在烏克蘭,我不太懂英語,這就是我意識到的。 這些都是可以添加到類中的異步方法,我想你會明白本質
// just variables
let cutesAudio = await this.cutAudio(audio, time, tempAudio);
let videoDuration = await this.getMediaDuration(video);
let mergeVideoAudio = await this.margeFiles(cutesAudio, video, tempVideo);
let cutMergeVideo = await this.cuteVideo(mergeVideoAudio, videoDuration, resultVideo);
// cut audio from a specific time and get temp.mp3
async cutAudio (audio, time, outputMp3) {
return new Promise((resolve, reject) => {
ffmpeg()
.input(audio)
.setStartTime(time)
.output(outputMp3)
.format('mp3')
.on('end', () => {
resolve(outputMp3);
}).on('error', (_err) => {
reject(_err);
}).run();
});
}
// get the video duration
async getMediaDuration (file) {
return new Promise((resolve, reject) => {
ffmpeg.ffprobe(file, (_err, metadata) => {
if (_err === null) {
resolve(metadata.format.duration);
} else {
reject(_err);
}
});
});
}
// connect the cut off temp.mp3 and the video and get temp.mp4
async margeFiles (audio, video, outputVideo) {
return new Promise((resolve, reject) => {
ffmpeg()
.videoCodec('libx264')
.format('mp4')
.outputFormat('mp4')
.input(audio)
.input(video)
.output(outputVideo)
.on('end', () => {
resolve(outputVideo);
}).on('error', (_err) => {
reject(_err);
}).run();
});
}
// we cut off the video we make it old length as the music can be longer.
async cuteVideo (video, time, result) {
return new Promise((resolve, reject) => {
ffmpeg()
.input(video)
.setDuration(time)
.format('mp4')
.output(result)
.on('end', () => {
resolve(result);
}).on('error', (_err) => {
reject(_err);
}).run();
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.