FFMPEG 如何复制音频文件或裁剪到特定长度
问题描述
我想要将一个有音频的视频文件和一个音频文件合并,得到一个输出文件。
最重要的是,我需要做到以下几点:
如果视频文件的长度更长,则需要增加音频文件的长度至相同。
如果音频文件长度比视频文件长,则需要将音频文件裁剪至与视频长度相同。
示例:
视频长度为2分5秒
音频长度为1分钟 – > 复制为2分5秒
若:
视频长度为1分钟
音频长度为2分5秒 – > 裁剪为1分钟
但是,我甚至无法增加音频文件的长度。
export async function overlayAudio(id: number, music: Music) {
console.log('start')
const videoPath = path.join(__dirname, `../../../uploads/movie/{id}/result/movie/predfinal.mp4`);
if (music === null) {
return videoPath.match(/\\uploads(.*)/)[0];
} else {
const audioPath = path.join(__dirname, `../../../{music.audio}`);
const outputVideoPath = path.join(__dirname, `../../../uploads/movie/{id}/result/movie/output.mp4`);
const matchPath = outputVideoPath.match(/\\uploads(.*)/);
const cmd = `ffmpeg -i{videoPath} -i {audioPath} -filter_complex "[0:a]volume=1[a];[1:a]volume=0.2[b];[b]apad[looped_audio];[a][looped_audio]amix=inputs=2:duration=longest" -c:v copy -c:a aac -strict experimental -shortest{outputVideoPath}`;
try {
await execPromise(cmd);
console.log('end!')
return matchPath[0];
} catch (error) {
console.error('Error:', error);
throw error;
}
}
}
解决方案
export async function overlayAudio(id: number, music: Music) {
console.log("start");
const videoPath = path.join(
__dirname,
`../../../uploads/movie/{id}/result/movie/predfinal.mp4`
);
if (music === null) {
return videoPath.match(/\\uploads(.*)/)[0];
} else {
const audioPath = path.join(__dirname, `../../../{music.audio}`);
const outputVideoPath = path.join(
__dirname,
`../../../uploads/movie/{id}/result/movie/output.mp4`
);
const matchPath = outputVideoPath.match(/\\uploads(.*)/);
const videoDurationCmd = `ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1{videoPath}`;
const audioDurationCmd = `ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 {audioPath}`;
const [videoDuration, audioDuration] = await Promise.all([
execPromise(videoDurationCmd),
execPromise(audioDurationCmd),
]);
// Calculate the time difference
const videoDurationValue = parseFloat(videoDuration);
const audioDurationValue = parseFloat(audioDuration);
const durationDifference = videoDurationValue - audioDurationValue;
let cmd;
if (durationDifference>0) { // If ideo is longer than audio
cmd = `ffmpeg -i{videoPath} -i {audioPath} -filter_complex "[0:a]volume=1[a];[1:a]volume=0.2[b];[b]apad[looped_audio];[a][looped_audio]amix=inputs=2:duration=first:dropout_transition=2" -c:v copy -c:a aac -strict experimental{outputVideoPath}`;
} else if (durationDifference < 0) { // If audio is longer than video
cmd = `ffmpeg -i {videoPath} -i{audioPath} -filter_complex "[0:a]volume=1[a];[1:a]volume=0.2[b];[b]apad[looped_audio];[a][looped_audio]amix=inputs=2:duration=first:dropout_transition=2" -c:v copy -c:a aac -strict experimental -t {videoDurationValue}{outputVideoPath}`;
} else { // If no need to adjust
cmd = `ffmpeg -i {videoPath} -i{audioPath} -filter_complex "[0:a]volume=1[a];[1:a]volume=0.2[b];[b]apad[looped_audio];[a][looped_audio]amix=inputs=2:duration=first:dropout_transition=2" -c:v copy -c:a aac -strict experimental ${outputVideoPath}`;
}
try {
await execPromise(cmd);
console.log("end!");
return matchPath[0];
} catch (error) {
console.error("Error:", error);
throw error;
}
}
}