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

Update egg-api.js

Browse files
Files changed (1) hide show
  1. egg-api.js +5 -13
egg-api.js CHANGED
@@ -76,11 +76,9 @@ app.get('/processGif', async (req, res) => {
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
 
@@ -122,7 +120,7 @@ app.get('/processGif', async (req, res) => {
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,16 +168,10 @@ app.get('/processGif', async (req, res) => {
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('内部服务器错误');
 
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
 
 
120
  const frameWidth = videoCapture.get(cv.CAP_PROP_FRAME_WIDTH);
121
  const frameHeight = videoCapture.get(cv.CAP_PROP_FRAME_HEIGHT);
122
  const originalFps = videoCapture.get(cv.CAP_PROP_FPS);
123
+ const videoWriter = new cv.VideoWriter(path.join(tempDir, 'out.mp4'), cv.VideoWriter.fourcc('avc1'), originalFps, new cv.Size(frameWidth, frameHeight));
124
  console.log('视频文件读取完成');
125
 
126
  // 处理每一帧
 
168
  cv.destroyAllWindows();
169
  console.log('帧处理完成');
170
 
171
+ await videoToGif(path.join(tempDir, 'out.mp4'), outputGifPath);
172
 
173
  res.set('Content-Type', 'image/gif');
174
  res.sendFile(outputGifPath);
 
 
 
 
 
 
175
  } catch (error) {
176
  console.error('处理GIF时出错:', error);
177
  res.status(500).send('内部服务器错误');