From 1b05aaeaf8732cbebe9a9723c6cd6b328da08f61 Mon Sep 17 00:00:00 2001 From: dm Date: Tue, 26 Jan 2021 01:38:46 +0100 Subject: [PATCH] add video conversion progress --- helpers/artifact_converter.js | 38 ++++++++++++++++++++++++++++++++++- views/partials/space.html | 2 +- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/helpers/artifact_converter.js b/helpers/artifact_converter.js index bc6be4f..0921755 100644 --- a/helpers/artifact_converter.js +++ b/helpers/artifact_converter.js @@ -50,6 +50,8 @@ const convertableAudioTypes = [ "audio/x-hx-aac-adts", "audio/aac"]; +// ffmpeg progress +var duration = 0, time = 0, progress = 0; function getDuration(localFilePath, callback){ exec.execFile("ffprobe", ["-show_format", "-of", "json", localFilePath], function(error, stdout, stderr) { @@ -58,6 +60,40 @@ function getDuration(localFilePath, callback){ }); } +function getConversionProgress(content){ + // get duration of source + var matches = (content) ? content.match(/Duration: (.*?), start:/) : []; + if( matches && matches.length>0 ){ + var rawDuration = matches[1]; + // convert rawDuration from 00:00:00.00 to seconds. + var ar = rawDuration.split(":").reverse(); + duration = parseFloat(ar[0]); + if (ar[1]) duration += parseInt(ar[1]) * 60; + if (ar[2]) duration += parseInt(ar[2]) * 60 * 60; + } + // get the time + matches = content.match(/time=(.*?) bitrate/g); + if( matches && matches.length>0 ){ + var rawTime = matches.pop(); + // needed if there is more than one match + if (Array.isArray(rawTime)){ + rawTime = rawTime.pop().replace('time=','').replace(' bitrate',''); + } else { + rawTime = rawTime.replace('time=','').replace(' bitrate',''); + } + + // convert rawTime from 00:00:00.00 to seconds. + ar = rawTime.split(":").reverse(); + time = parseFloat(ar[0]); + if (ar[1]) time += parseInt(ar[1]) * 60; + if (ar[2]) time += parseInt(ar[2]) * 60 * 60; + + //calculate the progress + progress = Math.round((time/duration) * 100); + } + return progress; +} + function createWaveform(fileName, localFilePath, callback){ var filePathImage = localFilePath + "-" + (new Date().getTime()) + ".png"; @@ -135,7 +171,7 @@ function convertVideo(fileName, filePath, codec, callback, progressCallback) { ff.stderr.on('data', function (data) { console.log('[ffmpeg-video] stderr: ' + data); if (progressCallback) { - progressCallback(data); + progressCallback(getConversionProgress(""+data)); } }); diff --git a/views/partials/space.html b/views/partials/space.html index 911d232..dbb8c65 100644 --- a/views/partials/space.html +++ b/views/partials/space.html @@ -157,7 +157,7 @@
-
{{a.description}}
+
{{a.description}}