1
1
import { join } from 'path'
2
2
import { existsSync } from 'fs'
3
3
import { spawn } from 'child_process'
4
+ import { app } from 'electron'
4
5
import { PythonScriptResult } from '../types'
5
6
import { generateFallbackTranscript } from './file.utils'
6
7
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
+
7
44
/**
8
45
* Extract transcript from video using Python script
9
46
*/
10
47
export async function extractTranscriptFromVideo ( videoPath : string ) : Promise < string > {
11
- return new Promise ( ( resolve ) => {
48
+ return new Promise ( async ( resolve ) => {
12
49
try {
13
50
console . log ( 'Running Python script to extract audio and transcribe...' )
14
51
console . log ( 'Video path:' , videoPath )
15
52
53
+ // Get the app path for both development and packaged environments
54
+ const appPath = app . isPackaged ? process . resourcesPath : process . cwd ( )
55
+
16
56
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
18
60
join ( process . cwd ( ) , 'audio_extractor.py' ) ,
61
+ // Relative to main process directory
62
+ join ( __dirname , '../../../audio_extractor.py' ) ,
63
+ // Try current directory as fallback
19
64
'audio_extractor.py'
20
65
]
21
66
67
+ console . log ( 'App is packaged:' , app . isPackaged )
68
+ console . log ( 'App path:' , appPath )
22
69
console . log ( 'Possible script paths:' , possiblePaths )
23
70
24
71
let scriptPath : string | null = null
25
72
for ( const path of possiblePaths ) {
73
+ console . log ( 'Checking path:' , path , 'exists:' , existsSync ( path ) )
26
74
if ( existsSync ( path ) ) {
27
75
scriptPath = path
28
76
console . log ( 'Found script at:' , scriptPath )
@@ -31,13 +79,22 @@ export async function extractTranscriptFromVideo(videoPath: string): Promise<str
31
79
}
32
80
33
81
if ( ! scriptPath ) {
34
- console . log ( 'No script found, using fallback' )
82
+ console . log ( 'No script found at any of the expected paths , using fallback' )
35
83
resolve ( generateFallbackTranscript ( videoPath ) )
36
84
return
37
85
}
38
86
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 ] )
41
98
42
99
let stdout = ''
43
100
let stderr = ''
0 commit comments