### Install get-video-duration Package Source: https://github.com/caffco/get-video-duration/blob/main/README.md Install the package using npm. This is a prerequisite for using the library. ```bash $ npm install --save get-video-duration ``` -------------------------------- ### Get Duration from Local File Path Source: https://context7.com/caffco/get-video-duration/llms.txt Use this function to get the duration of a local video file. Ensure the file path is correct and accessible. ```javascript const { getVideoDurationInSeconds } = require('get-video-duration'); const fs = require('fs'); // Get duration from a local file path async function getDurationFromFile() { try { const duration = await getVideoDurationInSeconds('./video.mp4'); console.log(`Video duration: ${duration} seconds`); // Output: Video duration: 596.474195 seconds } catch (error) { console.error('Failed to get duration:', error.message); } } ``` -------------------------------- ### Use Custom ffprobe Path Source: https://context7.com/caffco/get-video-duration/llms.txt Specify a custom path to the ffprobe binary if it's not in the system's PATH or if you are using a specific installation. This allows for more control over the ffprobe executable used. ```javascript // Use custom ffprobe path async function getDurationWithCustomFfprobe() { try { const duration = await getVideoDurationInSeconds( './video.mp4', '/usr/local/bin/ffprobe' ); console.log(`Duration: ${duration} seconds`); } catch (error) { console.error('Failed with custom ffprobe:', error.message); } } ``` -------------------------------- ### Get Video Duration from Readable Stream Source: https://github.com/caffco/get-video-duration/blob/main/README.md Process a video from a readable stream to get its duration. This is useful for handling videos piped from other sources. ```javascript const { getVideoDurationInSeconds } = require('get-video-duration') const fs = require('fs') const stream = fs.createReadStream('video.mov') getVideoDurationInSeconds(stream).then((duration) => { console.log(duration) }) ``` -------------------------------- ### Get Video Duration with Custom FFprobe Path Source: https://github.com/caffco/get-video-duration/blob/main/README.md Specify a custom path to the ffprobe executable if it's not in the system's PATH. This allows using a specific ffprobe version or location. ```javascript const { getVideoDurationInSeconds } = require('get-video-duration') // If you need to customize the path to ffprobe... getVideoDurationInSeconds('video.mov', '/path/to/ffprobe').then((duration) => { console.log(duration) }) ``` -------------------------------- ### Get Video Duration from URL Source: https://github.com/caffco/get-video-duration/blob/main/README.md Retrieve the duration of a video from a URL. Ensure the URL points to a directly accessible video file. ```javascript const { getVideoDurationInSeconds } = require('get-video-duration') // From a URL... getVideoDurationInSeconds( 'http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4' ).then((duration) => { console.log(duration) }) ``` -------------------------------- ### Get Video Duration from Local Path Source: https://github.com/caffco/get-video-duration/blob/main/README.md Use the getVideoDurationInSeconds function to get the duration of a local video file. The function returns a Promise that resolves with the duration in seconds. ```javascript const { getVideoDurationInSeconds } = require('get-video-duration') // From a local path... getVideoDurationInSeconds('video.mov').then((duration) => { console.log(duration) }) ``` -------------------------------- ### Get Duration from Remote URL Source: https://context7.com/caffco/get-video-duration/llms.txt Retrieve the duration of a video from a remote URL. This requires network access and the URL must point to a valid video file. ```javascript // Get duration from a remote URL async function getDurationFromURL() { try { const duration = await getVideoDurationInSeconds( 'https://storage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4' ); console.log(`Remote video duration: ${duration} seconds`); // Output: Remote video duration: 596.474195 seconds } catch (error) { console.error('Failed to get duration from URL:', error.message); } } ``` -------------------------------- ### Get Duration from Readable Stream Source: https://context7.com/caffco/get-video-duration/llms.txt Extract video duration from a Node.js readable stream. This is useful for processing video data directly from sources like file uploads without saving to disk. ```javascript // Get duration from a readable stream async function getDurationFromStream() { try { const stream = fs.createReadStream('./video.mp4'); const duration = await getVideoDurationInSeconds(stream); console.log(`Stream video duration: ${duration} seconds`); // Output: Stream video duration: 596.474195 seconds } catch (error) { console.error('Failed to get duration from stream:', error.message); } } ``` -------------------------------- ### Process Multiple Videos Concurrently Source: https://context7.com/caffco/get-video-duration/llms.txt Efficiently process durations for multiple video files by using `Promise.all`. This approach allows for parallel execution, significantly speeding up the process for large numbers of videos. ```javascript // Process multiple videos async function processMultipleVideos(videoPaths) { const results = await Promise.all( videoPaths.map(async (path) => { try { const duration = await getVideoDurationInSeconds(path); return { path, duration, error: null }; } catch (error) { return { path, duration: null, error: error.message }; } }) ); results.forEach(({ path, duration, error }) => { if (error) { console.log(`${path}: Error - ${error}`); } else { console.log(`${path}: ${duration} seconds`); } }); } ``` -------------------------------- ### ES Module / TypeScript Usage Source: https://context7.com/caffco/get-video-duration/llms.txt Demonstrates how to import and use `getVideoDurationInSeconds` in an ES Module or TypeScript environment. Note the type annotation for the duration variable. ```typescript // ES Module / TypeScript usage import getVideoDurationInSeconds from 'get-video-duration'; // or import { getVideoDurationInSeconds } from 'get-video-duration'; const duration: number = await getVideoDurationInSeconds('./video.mp4'); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.