11import { join } from 'path'
22import { existsSync } from 'fs'
33import { spawn } from 'child_process'
4+ import { app } from 'electron'
45import { PythonScriptResult } from '../types'
56import { generateFallbackTranscript } from './file.utils'
67
8+ /**
9+ * Find available Python command
10+ */
11+ function findPythonCommand ( ) : Promise < string | null > {
12+ const commands = [ 'python3' , 'python' , 'py' ]
13+
14+ return new Promise ( ( resolve ) => {
15+ let index = 0
16+
17+ function tryNext ( ) {
18+ if ( index >= commands . length ) {
19+ resolve ( null )
20+ return
21+ }
22+
23+ const cmd = commands [ index ++ ]
24+ const testProcess = spawn ( cmd , [ '--version' ] , { stdio : 'pipe' } )
25+
26+ testProcess . on ( 'close' , ( code ) => {
27+ if ( code === 0 ) {
28+ console . log ( `Found Python command: ${ cmd } ` )
29+ resolve ( cmd )
30+ } else {
31+ tryNext ( )
32+ }
33+ } )
34+
35+ testProcess . on ( 'error' , ( ) => {
36+ tryNext ( )
37+ } )
38+ }
39+
40+ tryNext ( )
41+ } )
42+ }
43+
744/**
845 * Extract transcript from video using Python script
946 */
1047export async function extractTranscriptFromVideo ( videoPath : string ) : Promise < string > {
11- return new Promise ( ( resolve ) => {
48+ return new Promise ( async ( resolve ) => {
1249 try {
1350 console . log ( 'Running Python script to extract audio and transcribe...' )
1451 console . log ( 'Video path:' , videoPath )
1552
53+ // Get the app path for both development and packaged environments
54+ const appPath = app . isPackaged ? process . resourcesPath : process . cwd ( )
55+
1656 const possiblePaths = [
17- join ( __dirname , '../../../audio_extractor.py' ) ,
57+ // For packaged app: resources/audio_extractor.py
58+ join ( appPath , 'audio_extractor.py' ) ,
59+ // For development: current working directory
1860 join ( process . cwd ( ) , 'audio_extractor.py' ) ,
61+ // Relative to main process directory
62+ join ( __dirname , '../../../audio_extractor.py' ) ,
63+ // Try current directory as fallback
1964 'audio_extractor.py'
2065 ]
2166
67+ console . log ( 'App is packaged:' , app . isPackaged )
68+ console . log ( 'App path:' , appPath )
2269 console . log ( 'Possible script paths:' , possiblePaths )
2370
2471 let scriptPath : string | null = null
2572 for ( const path of possiblePaths ) {
73+ console . log ( 'Checking path:' , path , 'exists:' , existsSync ( path ) )
2674 if ( existsSync ( path ) ) {
2775 scriptPath = path
2876 console . log ( 'Found script at:' , scriptPath )
@@ -31,13 +79,22 @@ export async function extractTranscriptFromVideo(videoPath: string): Promise<str
3179 }
3280
3381 if ( ! scriptPath ) {
34- console . log ( 'No script found, using fallback' )
82+ console . log ( 'No script found at any of the expected paths , using fallback' )
3583 resolve ( generateFallbackTranscript ( videoPath ) )
3684 return
3785 }
3886
39- console . log ( 'Spawning Python process with:' , scriptPath , videoPath )
40- const pythonProcess = spawn ( 'python' , [ scriptPath , videoPath ] )
87+ // Find available Python command
88+ const pythonCommand = await findPythonCommand ( )
89+
90+ if ( ! pythonCommand ) {
91+ console . log ( 'No Python command found, using fallback' )
92+ resolve ( generateFallbackTranscript ( videoPath ) )
93+ return
94+ }
95+
96+ console . log ( 'Spawning Python process with:' , pythonCommand , scriptPath , videoPath )
97+ const pythonProcess = spawn ( pythonCommand , [ scriptPath , videoPath ] )
4198
4299 let stdout = ''
43100 let stderr = ''
0 commit comments