Skip to content

Commit

Permalink
Add percent to ffmpeg functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sudospaes committed Jun 15, 2024
1 parent 5bc3b72 commit 4bd9354
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/cli/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,20 @@ export async function downloadAudio(link: string, tag: string) {

export function merging(video: IVideoObject, audio: IAudioObject) {
const path = `${video.title}-${video.quality}-${video.codec}.${video.extension}`;
let totalTime: number;
ffmpeg()
.mergeAdd(video.path)
.mergeAdd(audio.path)
.save(join(dirname(process.execPath), path))
.on("progress", () => {
.on("codecData", (data) => {
totalTime = parseInt(data.duration.replace(/:/g, ""));
})
.on("progress", (p) => {
const time = parseInt(p.timemark.replace(/:/g, ""));
const percent = (time / totalTime) * 100;
readline.clearLine(process.stdout, 0);
readline.cursorTo(process.stdout, 0);
process.stdout.write(`Merging video with audio...`);
process.stdout.write(`Merging video with audio - ${percent.toFixed(2)}%`);
})
.on("end", () => {
console.log("\nMerging Done :D");
Expand All @@ -168,17 +174,23 @@ export function merging(video: IVideoObject, audio: IAudioObject) {

export function conevrtToMp3(audio: IAudioObject) {
const path = `${audio.title}-${audio.bitrate}kb.mp3`;
let totalTime: number;
return new Promise<string>((resolve, reject) => {
ffmpeg()
.input(audio.path)
.format("mp3")
.audioBitrate(audio.bitrate)
.audioChannels(audio.channels)
.save(join(dirname(process.execPath), path))
.on("progress", () => {
.on("codecData", (data) => {
totalTime = parseInt(data.duration.replace(/:/g, ""));
})
.on("progress", (p) => {
const time = parseInt(p.timemark.replace(/:/g, ""));
const percent = (time / totalTime) * 100;
readline.clearLine(process.stdout, 0);
readline.cursorTo(process.stdout, 0);
process.stdout.write(`Converting to mp3...`);
process.stdout.write(`Converting to mp3 - ${percent.toFixed(2)}`);
})
.on("end", () => {
console.log("\nAudio converted to mp3 file :D");
Expand Down

0 comments on commit 4bd9354

Please sign in to comment.