Created
July 20, 2024 17:56
-
-
Save ishar19/3ee43064b0aa25c039e6ba6f0dcd1d74 to your computer and use it in GitHub Desktop.
code to get duration of a video
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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