Skip to content

Commit

Permalink
Merge branch 'feat/audio-waveform-thumbnails' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
nytamin committed Oct 19, 2023
2 parents 62eefdb + 3f49920 commit d35f5cc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -389,14 +389,21 @@ interface ThumbnailMetadata {
}
}
/** Returns arguments for FFMpeg to generate a thumbnail image file */
export function thumbnailFFMpegArguments(input: string, metadata: ThumbnailMetadata, seekTimeCode?: string): string[] {
export function thumbnailFFMpegArguments(
input: string,
metadata: ThumbnailMetadata,
seekTimeCode?: string,
hasVideoStream?: boolean
): string[] {
return [
'-hide_banner',
seekTimeCode ? `-ss ${seekTimeCode}` : undefined,
hasVideoStream && seekTimeCode ? `-ss ${seekTimeCode}` : undefined,
`-i "${input}"`,
`-f image2`,
'-frames:v 1',
`-vf ${!seekTimeCode ? 'thumbnail,' : ''}scale=${metadata.version.width}:${metadata.version.height}`,
hasVideoStream
? `-vf ${!seekTimeCode ? 'thumbnail,' : ''}scale=${metadata.version.width}:${metadata.version.height}` // Creates a thumbnail of the video.
: '-filter_complex "showwavespic=s=640x240:split_channels=1:colors=white"', // Creates an image of the audio waveform.
'-threads 1',
].filter(Boolean) as string[] // remove undefined values
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import {
} from './lib'
import { FFMpegProcess, spawnFFMpeg } from './lib/ffmpeg'
import { WindowsWorker } from '../windowsWorker'
import { CancelablePromise } from '../../../lib/cancelablePromise'
import { scanWithFFProbe, FFProbeScanResult } from './lib/scan'

/**
* Generates a thumbnail image from a source video file, and stores the resulting file into the target PackageContainer
Expand Down Expand Up @@ -149,9 +151,11 @@ export const MediaFileThumbnail: ExpectationWindowsHandler = {
if (!lookupTarget.ready) throw new Error(`Can't start working due to target: ${lookupTarget.reason.tech}`)

let ffMpegProcess: FFMpegProcess | undefined
let ffProbeProcess: CancelablePromise<any> | undefined
const workInProgress = new WorkInProgress({ workLabel: 'Generating thumbnail' }, async () => {
// On cancel
ffMpegProcess?.cancel()
ffProbeProcess?.cancel()
}).do(async () => {
if (
(lookupSource.accessor.type === Accessor.AccessType.LOCAL_FOLDER ||
Expand Down Expand Up @@ -221,8 +225,15 @@ export const MediaFileThumbnail: ExpectationWindowsHandler = {
throw new Error(`Unsupported Target AccessHandler`)
}

// Scan with FFProbe:
ffProbeProcess = scanWithFFProbe(sourceHandle)
const ffProbeScan: FFProbeScanResult = await ffProbeProcess
ffProbeProcess = undefined
const hasVideoStream =
ffProbeScan.streams && ffProbeScan.streams.some((stream) => stream.codec_type === 'video')

// Use FFMpeg to generate the thumbnail:
const args = thumbnailFFMpegArguments(inputPath, metadata, seekTimeCode)
const args = thumbnailFFMpegArguments(inputPath, metadata, seekTimeCode, hasVideoStream)

const fileOperation = await targetHandle.prepareForOperation('Generate thumbnail', lookupSource.handle)

Expand Down

0 comments on commit d35f5cc

Please sign in to comment.