如何解决Electron中FFmpeg打开输入文件报错的问题?
解决Electron中FFmpeg输入文件路径读取错误问题
问题根源
错误信息里的路径包含%20,这是URL编码后的空格字符,FFmpeg无法识别这种编码格式的路径,导致提示"文件不存在"。
解决方案
1. 解码URL编码的路径
使用JavaScript内置的decodeURIComponent()方法,将编码后的路径转换为系统可识别的普通路径:
const decodedVideoFile = decodeURIComponent(videoFile); const ffmpeg = spawn(ffmpegPath, [ '-i', decodedVideoFile, '-filter_complex', filterComplex, '-map', '[out]', '-c:v', 'libx264', '-crf', '18', '-preset', 'veryfast', '-y', path.join(outputDir, outputName) ]);
2. 提前验证文件存在性(可选)
为了避免其他路径问题,可以先检查解码后的路径是否真的存在:
const fs = require('fs'); const decodedVideoFile = decodeURIComponent(videoFile); if (!fs.existsSync(decodedVideoFile)) { console.error('输入文件不存在:', decodedVideoFile); event.reply('ffmpeg-export-error', '输入文件不存在'); return; } // 继续执行FFmpeg调用 const ffmpeg = spawn(ffmpegPath, [ '-i', decodedVideoFile, // 其他参数保持不变 ]);
3. 标准化路径格式(可选)
如果解码后仍有路径格式问题,可使用Node.js的path.normalize()方法统一路径格式:
const path = require('path'); const normalizedPath = path.normalize(decodeURIComponent(videoFile));
内容的提问来源于stack exchange,提问作者NiceShyGuy
相关产品推荐
相关产品推荐

