1
1
import path from "path" ;
2
2
import { exec , execSync } from "child_process" ;
3
+ import electronLog from 'electron-log' ;
3
4
import fs from "fs-extra" ;
4
5
import { app , BrowserWindow , ipcMain } from "electron" ;
5
6
import { format } from "date-fns" ;
6
7
import { getHtml , getAuthCmd , getExecuteFile , getExecutePath } from "./utils" ;
7
8
import { connectDataBase , findRecord , insertRecord } from "./sqlHelper" ;
8
9
import sharp from 'sharp'
9
10
process . env [ "ELECTRON_DISABLE_SECURITY_WARNINGS" ] = "true" ;
10
- let templateFilePath = path . join ( process . cwd ( ) , "resources" , "command" ) ;
11
-
12
-
13
- if ( import . meta. env . DEV ) {
14
- templateFilePath = path . join ( process . cwd ( ) , "command" ) ;
15
- }
16
- console . log ( templateFilePath , "templateFilePath" ) ;
17
-
18
- const ytDlp = path . join (
19
- process . cwd ( ) ,
20
- "command" ,
21
- getExecutePath ( ) ,
22
- getExecuteFile ( "yt-dlp" )
23
- ) ;
24
-
25
- const ytDlpPath = `${ getAuthCmd ( ) } ${ ytDlp } ` ;
26
-
27
- const ffmpeg = path . join (
28
- process . cwd ( ) ,
29
- "command" ,
30
- getExecutePath ( ) ,
31
- getExecuteFile ( "ffmpeg" )
32
- ) ;
33
-
34
- const ffmpegPath = `${ getAuthCmd ( ) } ${ ffmpeg } ` ;
35
-
36
- const removeDuplicateImages = path . join (
37
- process . cwd ( ) ,
38
- "command" ,
39
- getExecutePath ( ) ,
40
- getExecuteFile ( "RemoveDuplicateImages" )
41
- ) ;
42
-
43
- const removeDuplicateImagesPath = `${ getAuthCmd ( ) } ${ removeDuplicateImages } ` ;
44
11
45
12
// The built directory structure
46
13
//
@@ -107,8 +74,55 @@ function createWindow() {
107
74
// win.loadFile('dist/index.html')
108
75
win . loadFile ( path . join ( process . env . DIST , "index.html" ) ) ;
109
76
}
77
+
78
+ // 重写console.log方法
79
+ const originalConsoleLog = console . log ;
80
+ console . log = ( ...args ) => {
81
+ originalConsoleLog ( ...args ) ;
82
+ win ?. webContents . send ( 'main-process-log' , args ) ;
83
+ } ;
84
+
85
+ // 重写electron-log的日志方法
86
+ [ 'error' , 'warn' , 'info' , 'verbose' , 'debug' , 'silly' ] . forEach ( level => {
87
+ const originalLog = electronLog [ level ] ;
88
+ electronLog [ level ] = ( ...args ) => {
89
+ originalLog ( ...args ) ;
90
+ win ?. webContents . send ( 'main-process-log' , [ level , ...args ] ) ;
91
+ } ;
92
+ } ) ;
110
93
}
111
94
95
+ let templateFilePath = path . join ( process . cwd ( ) , "resources" , "command" ) ;
96
+
97
+ if ( ! import . meta. env . PROD ) {
98
+ templateFilePath = path . join ( process . cwd ( ) , "command" ) ;
99
+ }
100
+ console . log ( templateFilePath , "templateFilePath" ) ;
101
+
102
+ const ytDlp = path . join (
103
+ templateFilePath ,
104
+ getExecutePath ( ) ,
105
+ getExecuteFile ( "yt-dlp" )
106
+ ) ;
107
+
108
+ const ytDlpPath = `${ getAuthCmd ( ) } ${ ytDlp } ` ;
109
+
110
+ const ffmpeg = path . join (
111
+ templateFilePath ,
112
+ getExecutePath ( ) ,
113
+ getExecuteFile ( "ffmpeg" )
114
+ ) ;
115
+
116
+ const ffmpegPath = `${ getAuthCmd ( ) } ${ ffmpeg } ` ;
117
+
118
+ const removeDuplicateImages = path . join (
119
+ templateFilePath ,
120
+ getExecutePath ( ) ,
121
+ getExecuteFile ( "RemoveDuplicateImages" )
122
+ ) ;
123
+
124
+ const removeDuplicateImagesPath = `${ getAuthCmd ( ) } ${ removeDuplicateImages } ` ;
125
+
112
126
app . on ( "window-all-closed" , ( ) => {
113
127
app . quit ( ) ;
114
128
win = null ;
@@ -120,8 +134,7 @@ ipcMain.on("call-yt-dlp-video", async(event,videoUrl: string) => {
120
134
let record : any = await findRecord ( videoUrl ) ;
121
135
console . log ( record , "record-------------------" )
122
136
let locationPath = path . join (
123
- process . cwd ( ) ,
124
- "command" ,
137
+ templateFilePath ,
125
138
record . FolderDate
126
139
) ;
127
140
@@ -176,8 +189,7 @@ ipcMain.on("call-yt-dlp", async (event, videoUrl, isDownloadVideo) => {
176
189
const createInfo = createMetadata ( videoUrl ) ;
177
190
178
191
let locationPath = path . join (
179
- process . cwd ( ) ,
180
- "command" ,
192
+ templateFilePath ,
181
193
createInfo . folderDate
182
194
) ;
183
195
@@ -268,8 +280,7 @@ ipcMain.on(
268
280
const startTimeName = everyStartTime . replace ( / [ . : , - ] / g, "" ) ;
269
281
console . log ( startTimeName , "startTimeName" )
270
282
const imagePath = path . join (
271
- process . cwd ( ) ,
272
- "command" ,
283
+ templateFilePath ,
273
284
folderDate ,
274
285
startTimeName
275
286
) ;
@@ -309,13 +320,17 @@ ipcMain.on("call-get-duration", (event, folderDate) => {
309
320
event . reply ( "reply-duration" , packageJson . duration ) ;
310
321
} )
311
322
323
+ ipcMain . on ( "call-execute-path" , ( event ) => {
324
+ event . reply ( "reply-execute-path" , process . cwd ( ) ) ;
325
+ } )
326
+
312
327
/**
313
328
* 图片压缩
314
329
*/
315
330
ipcMain . on ( "call-image-compress" , ( event , folderDate , everyStartTime , list ) => {
316
331
console . log ( "call-image-compress" , list )
317
332
const startTimeName = everyStartTime . replace ( / [ . : , - ] / g, "" ) ;
318
- const locationPath = path . join ( process . cwd ( ) , "command" , folderDate , startTimeName ) ;
333
+ const locationPath = path . join ( templateFilePath , folderDate , startTimeName ) ;
319
334
list . forEach ( async ( item : any , index : number ) => {
320
335
console . log ( item , "fileName" )
321
336
let fileName = item . split ( '.' ) [ 0 ] ;
@@ -382,6 +397,7 @@ const removeSimilarImages = (imagePath: string, multiple: number) => {
382
397
/**
383
398
* 在指定目录下查找元数据json文件
384
399
* @param directoryPath
400
+ * @param type
385
401
* @returns
386
402
*/
387
403
const findJsonFilesInDirectorySync = (
@@ -425,12 +441,19 @@ const createMetadata = (url: string) => {
425
441
const folderDate = format ( new Date ( ) , "yyyy-MM-dd-HH-mm-ss" ) ;
426
442
console . log ( folderDate , "date-folderDate" ) ;
427
443
428
- const locationPath = path . join ( process . cwd ( ) , "command" , folderDate ) ;
444
+ const locationPath = path . join ( templateFilePath , folderDate ) ;
429
445
let cmd = "" ;
430
446
cmd = ` ${ ytDlpPath } ${ url } -P ${ locationPath } --write-info-json --skip-download -o "%(id)s.%(ext)s"` ;
431
447
432
448
console . log ( cmd , "cmd-123" ) ;
433
- execSync ( cmd ) ;
449
+ try
450
+ {
451
+ execSync ( cmd ) ;
452
+ }
453
+ catch ( e ) {
454
+ console . log ( "执行cmd-123失败" , e ) ;
455
+ }
456
+ console . log ( "cmd-123执行完毕" ) ;
434
457
const jsonFile : string | undefined =
435
458
findJsonFilesInDirectorySync ( locationPath ) ;
436
459
0 commit comments