Skip to content

Instantly share code, notes, and snippets.

@ishar19
Created July 20, 2024 17:56
Show Gist options
  • Save ishar19/3ee43064b0aa25c039e6ba6f0dcd1d74 to your computer and use it in GitHub Desktop.
Save ishar19/3ee43064b0aa25c039e6ba6f0dcd1d74 to your computer and use it in GitHub Desktop.
code to get duration of a video
const { execFile } = require('child_process');
const { promisify } = require('util');
const execFilePromise = promisify(execFile);
async function getVideoDuration(filePath) {
try {
const { stdout } = await execFilePromise('ffprobe', [
'-v',
'error',
'-show_entries',
'format=duration',
'-of',
'default=noprint_wrappers=1:nokey=1',
filePath
]);
// Parse the output to a float and round to 2 decimal places
const duration = parseFloat(stdout).toFixed(2);
return `${duration} seconds`;
} catch (error) {
console.error('Error getting video duration:', error);
return null;
}
}
// Example usage
(async () => {
const duration = await getVideoDuration('./video.mp4'); // your video path, relative
console.log('Video duration:', duration);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment