Konversi Video atau Voice Note ke MP3 dengan JavaScript
Berikut adalah contoh kode JavaScript untuk mengubah file video atau voice note menjadi format MP3 menggunakan library 'ffmpeg':
const ffmpegPath = require('ffmpeg-static').path;
const ffmpeg = require('fluent-ffmpeg');
ffmpeg.setFfmpegPath(ffmpegPath);
function convertToMp3(inputPath, outputPath) {
return new Promise((resolve, reject) => {
ffmpeg(inputPath)
.output(outputPath)
.on('end', resolve)
.on('error', reject)
.run();
});
}
// Contoh penggunaan
const inputPath = 'path/to/input/file'; // Ganti dengan path file input yang sesuai
const outputPath = 'path/to/output/file.mp3'; // Ganti dengan path file output yang diinginkan
convertToMp3(inputPath, outputPath)
.then(() => {
console.log('File berhasil diubah menjadi mp3');
})
.catch((error) => {
console.error('Terjadi kesalahan:', error);
});
Pastikan Anda telah menginstal library 'ffmpeg' dan 'fluent-ffmpeg' sebelum menjalankan kode di atas. Anda dapat menginstalnya melalui terminal dengan perintah berikut:
npm install ffmpeg fluent-ffmpeg ffmpeg-static
Harap diingat bahwa kode di atas hanya dapat mengonversi file ke format MP3 jika format sumber file (video/voice note) didukung oleh 'ffmpeg'.
原文地址: https://www.cveoy.top/t/topic/qo7E 著作权归作者所有。请勿转载和采集!