mmdhx commited on
Commit
ceb1c28
·
verified ·
1 Parent(s): 1a372c0

Update egg-api.js

Browse files
Files changed (1) hide show
  1. egg-api.js +26 -8
egg-api.js CHANGED
@@ -22,7 +22,7 @@ async function gifToVideo(inputGifPath, outputVideoPath) {
22
  console.log('FFmpeg 命令:', commandLine);
23
  })
24
  .on('end', () => {
25
- console.log('GIF成功转换为视频');
26
  resolve();
27
  })
28
  .on('error', (err) => {
@@ -43,7 +43,7 @@ async function videoToGif(inputVideoPath, outputGifPath) {
43
  console.log('FFmpeg 命令:', commandLine);
44
  })
45
  .on('end', () => {
46
- console.log('视频成功转换为GIF');
47
  resolve();
48
  })
49
  .on('error', (err) => {
@@ -76,9 +76,11 @@ app.get('/processGif', async (req, res) => {
76
  try {
77
  const { gifUrl } = req.query;
78
  const tempDir = os.tmpdir();
79
- const tempGifPath = path.join(tempDir, 'temp.gif');
80
- const tempVideoPath = path.join(tempDir, 'temp_video.mp4');
81
- const outputGifPath = path.join(tempDir, 'output.gif');
 
 
82
  const replacementImagePath = path.join(__dirname, 'replacement_face.png');
83
  const classifiersFolder = path.join(__dirname, 'classifiers');
84
 
@@ -96,7 +98,7 @@ app.get('/processGif', async (req, res) => {
96
  writer.on('error', reject);
97
  });
98
 
99
- console.log('GIF下载完成');
100
 
101
  // 从文件夹动态加载分类器
102
  const classifiers = await loadClassifiersFromFolder(classifiersFolder);
@@ -104,13 +106,23 @@ app.get('/processGif', async (req, res) => {
104
  // 将GIF转换为视频
105
  await gifToVideo(tempGifPath, tempVideoPath);
106
 
 
 
 
 
 
107
  // 读取视频文件
108
  console.log('开始读取视频文件...');
109
  const videoCapture = new cv.VideoCapture(tempVideoPath);
 
 
 
 
 
110
  const frameWidth = videoCapture.get(cv.CAP_PROP_FRAME_WIDTH);
111
  const frameHeight = videoCapture.get(cv.CAP_PROP_FRAME_HEIGHT);
112
  const originalFps = videoCapture.get(cv.CAP_PROP_FPS);
113
- const videoWriter = new cv.VideoWriter(path.join(tempDir, 'out.mp4'), cv.VideoWriter.fourcc('avc1'), originalFps, new cv.Size(frameWidth, frameHeight));
114
  console.log('视频文件读取完成');
115
 
116
  // 处理每一帧
@@ -158,10 +170,16 @@ app.get('/processGif', async (req, res) => {
158
  cv.destroyAllWindows();
159
  console.log('帧处理完成');
160
 
161
- await videoToGif(path.join(tempDir, 'out.mp4'), outputGifPath);
162
 
163
  res.set('Content-Type', 'image/gif');
164
  res.sendFile(outputGifPath);
 
 
 
 
 
 
165
  } catch (error) {
166
  console.error('处理GIF时出错:', error);
167
  res.status(500).send('内部服务器错误');
 
22
  console.log('FFmpeg 命令:', commandLine);
23
  })
24
  .on('end', () => {
25
+ console.log('GIF成功转换为视频:', outputVideoPath);
26
  resolve();
27
  })
28
  .on('error', (err) => {
 
43
  console.log('FFmpeg 命令:', commandLine);
44
  })
45
  .on('end', () => {
46
+ console.log('视频成功转换为GIF:', outputGifPath);
47
  resolve();
48
  })
49
  .on('error', (err) => {
 
76
  try {
77
  const { gifUrl } = req.query;
78
  const tempDir = os.tmpdir();
79
+ const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
80
+ const tempGifPath = path.join(tempDir, `temp-${uniqueSuffix}.gif`);
81
+ const tempVideoPath = path.join(tempDir, `temp_video-${uniqueSuffix}.mp4`);
82
+ const outputGifPath = path.join(tempDir, `output-${uniqueSuffix}.gif`);
83
+ const outputVideoPath = path.join(tempDir, `out-${uniqueSuffix}.mp4`);
84
  const replacementImagePath = path.join(__dirname, 'replacement_face.png');
85
  const classifiersFolder = path.join(__dirname, 'classifiers');
86
 
 
98
  writer.on('error', reject);
99
  });
100
 
101
+ console.log('GIF下载完成:', tempGifPath);
102
 
103
  // 从文件夹动态加载分类器
104
  const classifiers = await loadClassifiersFromFolder(classifiersFolder);
 
106
  // 将GIF转换为视频
107
  await gifToVideo(tempGifPath, tempVideoPath);
108
 
109
+ // 确认视频文件存在
110
+ if (!fs.existsSync(tempVideoPath)) {
111
+ throw new Error(`视频文件不存在: ${tempVideoPath}`);
112
+ }
113
+
114
  // 读取视频文件
115
  console.log('开始读取视频文件...');
116
  const videoCapture = new cv.VideoCapture(tempVideoPath);
117
+
118
+ if (!videoCapture.isOpened()) {
119
+ throw new Error(`无法打开视频文件: ${tempVideoPath}`);
120
+ }
121
+
122
  const frameWidth = videoCapture.get(cv.CAP_PROP_FRAME_WIDTH);
123
  const frameHeight = videoCapture.get(cv.CAP_PROP_FRAME_HEIGHT);
124
  const originalFps = videoCapture.get(cv.CAP_PROP_FPS);
125
+ const videoWriter = new cv.VideoWriter(outputVideoPath, cv.VideoWriter.fourcc('avc1'), originalFps, new cv.Size(frameWidth, frameHeight));
126
  console.log('视频文件读取完成');
127
 
128
  // 处理每一帧
 
170
  cv.destroyAllWindows();
171
  console.log('帧处理完成');
172
 
173
+ await videoToGif(outputVideoPath, outputGifPath);
174
 
175
  res.set('Content-Type', 'image/gif');
176
  res.sendFile(outputGifPath);
177
+
178
+ // 删除临时文件
179
+ fs.unlinkSync(tempGifPath);
180
+ fs.unlinkSync(tempVideoPath);
181
+ fs.unlinkSync(outputVideoPath);
182
+ fs.unlinkSync(outputGifPath);
183
  } catch (error) {
184
  console.error('处理GIF时出错:', error);
185
  res.status(500).send('内部服务器错误');