-
Notifications
You must be signed in to change notification settings - Fork 23
First-pass native video ffmpeg display setting to prevent transcoding, when selected #1567
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mattdawkins
wants to merge
8
commits into
main
Choose a base branch
from
add-native-option
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
44196de
First-pass native video setting to prevent transcoding necessity
mattdawkins b929230
Remove unused import and await in loop
mattdawkins c2f3cd0
Fix runtime error
mattdawkins 402c4d2
Switch to canvas to prevent black display
mattdawkins 73a7e8d
Fix linting issue
mattdawkins 972649a
Correct ordering issue
mattdawkins c6f2820
Fix test
mattdawkins 03e83a0
Only load native annotator in desktop mode
mattdawkins File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
257 changes: 257 additions & 0 deletions
257
client/platform/desktop/backend/native/frameExtraction.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,257 @@ | ||
| /** | ||
| * Frame extraction service for native video playback. | ||
| * Extracts frames on-demand using FFmpeg without requiring | ||
| * full video transcoding. | ||
| */ | ||
| import { spawn } from 'child_process'; | ||
| import { getBinaryPath, spawnResult } from './utils'; | ||
|
|
||
| const ffmpegPath = getBinaryPath('ffmpeg-ffprobe-static/ffmpeg'); | ||
| const ffprobePath = getBinaryPath('ffmpeg-ffprobe-static/ffprobe'); | ||
|
|
||
| // LRU cache for extracted frames | ||
| interface CachedFrame { | ||
| data: Buffer; | ||
| timestamp: number; | ||
| } | ||
|
|
||
| interface FrameCache { | ||
| frames: Map<number, CachedFrame>; | ||
| maxSize: number; | ||
| } | ||
|
|
||
| const frameCaches: Map<string, FrameCache> = new Map(); | ||
| const MAX_CACHE_SIZE = 100; // Max frames per video | ||
| const CACHE_EXPIRY_MS = 5 * 60 * 1000; // 5 minutes | ||
|
|
||
| interface VideoInfo { | ||
| fps: number; | ||
| duration: number; | ||
| width: number; | ||
| height: number; | ||
| frameCount: number; | ||
| } | ||
|
|
||
| /** | ||
| * Get video information using ffprobe | ||
| */ | ||
| async function getVideoInfo(videoPath: string): Promise<VideoInfo> { | ||
| const args = [ | ||
| '-v', 'quiet', | ||
| '-print_format', 'json', | ||
| '-show_format', | ||
| '-show_streams', | ||
| videoPath, | ||
| ]; | ||
|
|
||
| const result = await spawnResult(ffprobePath, args); | ||
| if (result.error || result.output === null) { | ||
| throw new Error(`Failed to probe video: ${result.error || 'Unknown error'}`); | ||
| } | ||
|
|
||
| const info = JSON.parse(result.output); | ||
| const videoStream = info.streams?.find((s: { codec_type: string }) => s.codec_type === 'video'); | ||
|
|
||
| if (!videoStream) { | ||
| throw new Error('No video stream found'); | ||
| } | ||
|
|
||
| // Parse frame rate (e.g., "30000/1001" or "30/1") | ||
| const fpsStr = videoStream.avg_frame_rate || videoStream.r_frame_rate || '30/1'; | ||
| const [num, den] = fpsStr.split('/').map(Number); | ||
| const fps = den ? num / den : num; | ||
|
|
||
| const duration = parseFloat(info.format?.duration || videoStream.duration || '0'); | ||
| const frameCount = Math.floor(duration * fps); | ||
|
|
||
| return { | ||
| fps, | ||
| duration, | ||
| width: videoStream.width || 0, | ||
| height: videoStream.height || 0, | ||
| frameCount, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Get or create a frame cache for a video | ||
| */ | ||
| function getFrameCache(videoPath: string): FrameCache { | ||
| let cache = frameCaches.get(videoPath); | ||
| if (!cache) { | ||
| cache = { | ||
| frames: new Map(), | ||
| maxSize: MAX_CACHE_SIZE, | ||
| }; | ||
| frameCaches.set(videoPath, cache); | ||
| } | ||
| return cache; | ||
| } | ||
|
|
||
| /** | ||
| * Evict old entries from the cache | ||
| */ | ||
| function evictOldEntries(cache: FrameCache) { | ||
| const now = Date.now(); | ||
| const entries = Array.from(cache.frames.entries()); | ||
|
|
||
| // Remove expired entries | ||
| entries.forEach(([frame, cached]) => { | ||
| if (now - cached.timestamp > CACHE_EXPIRY_MS) { | ||
| cache.frames.delete(frame); | ||
| } | ||
| }); | ||
|
|
||
| // If still over capacity, remove oldest entries | ||
| if (cache.frames.size > cache.maxSize) { | ||
| const sortedEntries = entries | ||
| .filter(([frame]) => cache.frames.has(frame)) | ||
| .sort((a, b) => a[1].timestamp - b[1].timestamp); | ||
|
|
||
| const toRemove = sortedEntries.slice(0, cache.frames.size - cache.maxSize); | ||
| toRemove.forEach(([frame]) => cache.frames.delete(frame)); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Extract a single frame from a video at the specified frame number | ||
| * Returns the frame as a JPEG buffer | ||
| */ | ||
| async function extractFrame( | ||
| videoPath: string, | ||
| frameNumber: number, | ||
| fps: number, | ||
| ): Promise<Buffer> { | ||
| const cache = getFrameCache(videoPath); | ||
|
|
||
| // Check cache first | ||
| const cached = cache.frames.get(frameNumber); | ||
| if (cached) { | ||
| cached.timestamp = Date.now(); // Update access time | ||
| return cached.data; | ||
| } | ||
|
|
||
| // Calculate timestamp from frame number | ||
| const timestamp = frameNumber / fps; | ||
|
|
||
| // Extract frame using ffmpeg | ||
| // Use -ss before -i for fast seeking to nearest keyframe | ||
| // Then use accurate seeking with filter | ||
| const args = [ | ||
| '-ss', timestamp.toFixed(6), | ||
| '-i', videoPath, | ||
| '-vframes', '1', | ||
| '-f', 'image2pipe', | ||
| '-vcodec', 'mjpeg', | ||
| '-q:v', '2', // High quality JPEG | ||
| '-', | ||
| ]; | ||
|
|
||
| return new Promise((resolve, reject) => { | ||
| const ffmpeg = spawn(ffmpegPath, args); | ||
| const chunks: Buffer[] = []; | ||
|
|
||
| ffmpeg.stdout.on('data', (chunk: Buffer) => { | ||
| chunks.push(chunk); | ||
| }); | ||
|
|
||
| ffmpeg.stderr.on('data', () => { | ||
| // Ignore stderr output (progress info) | ||
| }); | ||
|
|
||
| ffmpeg.on('close', (code) => { | ||
| if (code === 0 && chunks.length > 0) { | ||
| const frameData = Buffer.concat(chunks); | ||
|
|
||
| // Cache the frame | ||
| cache.frames.set(frameNumber, { | ||
| data: frameData, | ||
| timestamp: Date.now(), | ||
| }); | ||
| evictOldEntries(cache); | ||
|
|
||
| resolve(frameData); | ||
| } else { | ||
| reject(new Error(`FFmpeg exited with code ${code}`)); | ||
| } | ||
| }); | ||
|
|
||
| ffmpeg.on('error', (err) => { | ||
| reject(err); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Process frames in batches with limited concurrency | ||
| */ | ||
| async function processBatches( | ||
| batches: number[][], | ||
| videoPath: string, | ||
| fps: number, | ||
| ): Promise<void> { | ||
| if (batches.length === 0) return; | ||
| const [batch, ...remaining] = batches; | ||
| await Promise.all(batch.map((frame) => extractFrame(videoPath, frame, fps).catch(() => null))); | ||
| await processBatches(remaining, videoPath, fps); | ||
| } | ||
|
|
||
| /** | ||
| * Pre-extract multiple frames around a given frame (for smoother playback) | ||
| */ | ||
| async function prefetchFrames( | ||
| videoPath: string, | ||
| centerFrame: number, | ||
| fps: number, | ||
| range: number = 5, | ||
| ): Promise<void> { | ||
| const cache = getFrameCache(videoPath); | ||
| const framesToFetch: number[] = []; | ||
|
|
||
| // Determine which frames to prefetch (not already cached) | ||
| for (let i = -range; i <= range; i += 1) { | ||
| const frame = centerFrame + i; | ||
| if (frame >= 0 && !cache.frames.has(frame)) { | ||
| framesToFetch.push(frame); | ||
| } | ||
| } | ||
|
|
||
| // Fetch in parallel (limited concurrency) | ||
| const concurrency = 3; | ||
| const batches: number[][] = []; | ||
| for (let i = 0; i < framesToFetch.length; i += concurrency) { | ||
| batches.push(framesToFetch.slice(i, i + concurrency)); | ||
| } | ||
| await processBatches(batches, videoPath, fps); | ||
| } | ||
|
|
||
| /** | ||
| * Clear the frame cache for a specific video | ||
| */ | ||
| function clearCache(videoPath?: string) { | ||
| if (videoPath) { | ||
| frameCaches.delete(videoPath); | ||
| } else { | ||
| frameCaches.clear(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get cache statistics | ||
| */ | ||
| function getCacheStats(videoPath: string): { cachedFrames: number; maxSize: number } { | ||
| const cache = frameCaches.get(videoPath); | ||
| return { | ||
| cachedFrames: cache?.frames.size || 0, | ||
| maxSize: MAX_CACHE_SIZE, | ||
| }; | ||
| } | ||
|
|
||
| export { | ||
| extractFrame, | ||
| prefetchFrames, | ||
| getVideoInfo, | ||
| clearCache, | ||
| getCacheStats, | ||
| VideoInfo, | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd have to look into how this specific ffmpeg comand works (in extractFrame) but I'm guessing you probably wouldn't want to be spawning a new process for each frame because of the overhead. There should be an option to be able to a range of frames in a single process.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a follow-up to address this with an optional setting, but it depends on code in the other branch landing first (the multi-camera option)