@@ -3,6 +3,7 @@ import type { IpcMainEvent } from 'electron'
33import { Buffer } from 'node:buffer'
44import { spawn } from 'node:child_process'
55import path from 'node:path'
6+ import { IpcChannelOn } from '@shared/constant/ipc'
67import iconv from 'iconv-lite'
78import { addProcess , removeProcess } from './childProcessManager'
89import { getExecPath , getGenVpyPath } from './getCorePath'
@@ -12,8 +13,8 @@ export async function preview(event: IpcMainEvent, taskConfig: TaskConfig): Prom
1213 const vspipePath = getExecPath ( ) . vspipe
1314
1415 if ( ! taskConfig . fileList || taskConfig . fileList . length === 0 ) {
15- event . sender . send ( 'ffmpeg-output' , '错误: 没有提供用于预览的文件。\n' )
16- event . sender . send ( 'ffmpeg-finish' )
16+ event . sender . send ( IpcChannelOn . FFMPEG_OUTPUT , '错误: 没有提供用于预览的文件。\n' )
17+ event . sender . send ( IpcChannelOn . FFMPEG_FINISHED )
1718 return
1819 }
1920
@@ -38,7 +39,7 @@ export async function preview(event: IpcMainEvent, taskConfig: TaskConfig): Prom
3839 }
3940 await new Promise < void > ( ( resolve , reject ) => {
4041 const vspipeInfoProcess = spawn ( vspipePath , [ '--info' , vpyPath ] )
41- addProcess ( vspipeInfoProcess )
42+ addProcess ( 'vspipe' , vspipeInfoProcess )
4243
4344 let vspipeOut = '' // 用于保存 stdout 内容
4445 // eslint-disable-next-line unused-imports/no-unused-vars
@@ -47,36 +48,36 @@ export async function preview(event: IpcMainEvent, taskConfig: TaskConfig): Prom
4748 vspipeInfoProcess . stdout . on ( 'data' , ( data : Buffer ) => {
4849 const str = iconv . decode ( data , 'gbk' )
4950 vspipeOut += str
50- event . sender . send ( 'ffmpeg-output' , `${ str } ` )
51+ event . sender . send ( IpcChannelOn . FFMPEG_OUTPUT , `${ str } ` )
5152 } )
5253
5354 vspipeInfoProcess . stderr . on ( 'data' , ( data : Buffer ) => {
5455 const str = iconv . decode ( data , 'gbk' )
5556 stderrOut += str
56- event . sender . send ( 'ffmpeg-output' , `${ str } ` )
57+ event . sender . send ( IpcChannelOn . FFMPEG_OUTPUT , `${ str } ` )
5758 } )
5859
5960 vspipeInfoProcess . on ( 'close' , ( code ) => {
6061 removeProcess ( vspipeInfoProcess )
61- event . sender . send ( 'ffmpeg-output' , `vspipe info 执行完毕,退出码: ${ code } \n` ) ///////
62+ event . sender . send ( IpcChannelOn . FFMPEG_OUTPUT , `vspipe info 执行完毕,退出码: ${ code } \n` ) ///////
6263 info = {
6364 width : vspipeOut . match ( / W i d t h : \s * ( \d + ) / ) ?. [ 1 ] || '未知' ,
6465 height : vspipeOut . match ( / H e i g h t : \s * ( \d + ) / ) ?. [ 1 ] || '未知' ,
6566 frames : vspipeOut . match ( / F r a m e s : \s * ( \d + ) / ) ?. [ 1 ] || '0' ,
6667 fps : vspipeOut . match ( / F P S : \s * ( [ \d / ] + ) \s * \( ( [ \d . ] + ) f p s \) / ) ?. [ 2 ] || '0' ,
6768 }
6869
69- event . sender . send ( 'preview-info' , info )
70+ event . sender . send ( IpcChannelOn . PREVIEW_INFO , info )
7071 resolve ( )
7172 } )
7273
7374 vspipeInfoProcess . on ( 'error' , ( err ) => {
74- event . sender . send ( 'ffmpeg-output' , `vspipe 执行出错: ${ err . message } \n` )
75+ event . sender . send ( IpcChannelOn . FFMPEG_OUTPUT , `vspipe 执行出错: ${ err . message } \n` )
7576 reject ( err )
7677 } )
7778 } )
78- event . sender . send ( 'preview-vpyPath' , vpyPath )
79- event . sender . send ( 'ffmpeg-finish' )
79+ event . sender . send ( IpcChannelOn . PREVIEW_VPY_PATH , vpyPath )
80+ event . sender . send ( IpcChannelOn . FFMPEG_FINISHED )
8081}
8182
8283export async function previewFrame ( event : IpcMainEvent , vpyPath : string , currentFrame : number ) : Promise < void > {
@@ -87,7 +88,7 @@ export async function previewFrame(event: IpcMainEvent, vpyPath: string, current
8788 const cmd = `"${ vspipePath } " -c y4m --start ${ currentFrame } --end ${ currentFrame } "${ vpyPath } " - | "${ ffmpegPath } " -y -f yuv4mpegpipe -i - -frames:v 1 -vcodec png -f image2pipe -`
8889
8990 const vspipePreviewProcess = spawn ( cmd , { shell : true } )
90- addProcess ( vspipePreviewProcess )
91+ addProcess ( 'vspipe' , vspipePreviewProcess )
9192
9293 const chunks : Buffer [ ] = [ ]
9394
@@ -97,24 +98,24 @@ export async function previewFrame(event: IpcMainEvent, vpyPath: string, current
9798
9899 vspipePreviewProcess . stderr . on ( 'data' , ( data : Buffer ) => {
99100 const str = iconv . decode ( data , 'gbk' )
100- event . sender . send ( 'ffmpeg-output' , str )
101+ event . sender . send ( IpcChannelOn . FFMPEG_OUTPUT , str )
101102 } )
102103
103104 vspipePreviewProcess . on ( 'close' , ( code ) => {
104105 removeProcess ( vspipePreviewProcess )
105106 if ( code === 0 ) {
106107 const buffer = Buffer . concat ( chunks )
107108 const base64 = `data:image/png;base64,${ buffer . toString ( 'base64' ) } `
108- event . sender . send ( 'preview-image' , base64 )
109+ event . sender . send ( IpcChannelOn . PREVIEW_IMAGE , base64 )
109110 }
110111 else {
111- event . sender . send ( 'ffmpeg-output' , `预览失败,退出码: ${ code } ` )
112- event . sender . send ( 'preview-image' , null )
112+ event . sender . send ( IpcChannelOn . FFMPEG_OUTPUT , `预览失败,退出码: ${ code } ` )
113+ event . sender . send ( IpcChannelOn . PREVIEW_IMAGE , null )
113114 }
114- event . sender . send ( 'ffmpeg-finish' )
115+ event . sender . send ( IpcChannelOn . FFMPEG_FINISHED )
115116 } )
116117
117118 vspipePreviewProcess . on ( 'error' , ( err ) => {
118- event . sender . send ( 'ffmpeg-output' , `命令执行出错: ${ err . message } ` )
119+ event . sender . send ( IpcChannelOn . FFMPEG_OUTPUT , `命令执行出错: ${ err . message } ` )
119120 } )
120121}
0 commit comments