|
|
@@ -898,9 +898,14 @@ export class Utility {
|
|
|
try {
|
|
|
if (hasCover) {
|
|
|
//提取封面
|
|
|
- await getFFmpegCover(inputPath, imagePath);
|
|
|
- imagePath = fileUri.getUriFromPath(imagePath)
|
|
|
- videoItem.pixelMapPath = imagePath;
|
|
|
+ let isSuccess:boolean= await getFFmpegCover(inputPath, imagePath);
|
|
|
+
|
|
|
+ if(isSuccess){
|
|
|
+ imagePath = fileUri.getUriFromPath(imagePath)
|
|
|
+ }else{
|
|
|
+ imagePath = ''
|
|
|
+ }
|
|
|
+ videoItem.pixelMapPath = imagePath;
|
|
|
}else if(Utility.isVideoByExtension(inputPath)){
|
|
|
//提取封面
|
|
|
await getVideoFFmpegCover(inputPath, imagePath);
|
|
|
@@ -1565,22 +1570,51 @@ function getFileNameWithoutExtension(filePath: string): string {
|
|
|
* @param inputPath 音乐文件路径
|
|
|
* @returns Promise<void>
|
|
|
*/
|
|
|
-async function getFFmpegCover(inputPath: string, outputPath: string) {
|
|
|
+// async function getFFmpegCover(inputPath: string, outputPath: string) : Promise<boolean>{
|
|
|
+// let commands = ["ffmpeg", "-y","-i", inputPath, "-map", "0:v", "-c:v", "copy", outputPath];
|
|
|
+// FFmpeg.execute(commands, {
|
|
|
+// logCallback: (logLevel: number, logMessage: string) => {
|
|
|
+// console.info(`[FFmpeg LOG] [${logLevel}]${logMessage}`)
|
|
|
+// },
|
|
|
+// progressCallback: (message: string) => {
|
|
|
+// console.info(`[FFmpeg progress]${JSON.stringify(FFProgressMessageParser.parse(message))}`)
|
|
|
+// },
|
|
|
+// })
|
|
|
+// .then(() => {
|
|
|
+// return true
|
|
|
+// console.info("FFmpeg execution succeeded.");
|
|
|
+// })
|
|
|
+// .catch((error: Error) => {
|
|
|
+// return false
|
|
|
+// console.error(`FFmpeg execution failed with error: ${error.message}`);
|
|
|
+// });
|
|
|
+// return ;
|
|
|
+// }
|
|
|
+
|
|
|
+/**
|
|
|
+ * 从音乐文件中提取封面
|
|
|
+ * @param inputPath 音乐文件路径
|
|
|
+ * @param outputPath 封面输出路径
|
|
|
+ * @returns Promise<boolean> 表示操作是否成功
|
|
|
+ */
|
|
|
+async function getFFmpegCover(inputPath: string, outputPath: string): Promise<boolean> {
|
|
|
let commands = ["ffmpeg", "-y","-i", inputPath, "-map", "0:v", "-c:v", "copy", outputPath];
|
|
|
- FFmpeg.execute(commands, {
|
|
|
- logCallback: (logLevel: number, logMessage: string) => {
|
|
|
- console.info(`[FFmpeg LOG] [${logLevel}]${logMessage}`)
|
|
|
- },
|
|
|
- progressCallback: (message: string) => {
|
|
|
- console.info(`[FFmpeg progress]${JSON.stringify(FFProgressMessageParser.parse(message))}`)
|
|
|
- },
|
|
|
- })
|
|
|
- .then(() => {
|
|
|
- console.info("FFmpeg execution succeeded.");
|
|
|
- })
|
|
|
- .catch((error: Error) => {
|
|
|
- console.error(`FFmpeg execution failed with error: ${error.message}`);
|
|
|
+
|
|
|
+ try {
|
|
|
+ await FFmpeg.execute(commands, {
|
|
|
+ logCallback: (logLevel: number, logMessage: string) => {
|
|
|
+ console.info(`[FFmpeg LOG] [${logLevel}] ${logMessage}`);
|
|
|
+ },
|
|
|
+ progressCallback: (message: string) => {
|
|
|
+ console.info(`[FFmpeg progress] ${JSON.stringify(FFProgressMessageParser.parse(message))}`);
|
|
|
+ },
|
|
|
});
|
|
|
+ console.info("FFmpeg execution succeeded.");
|
|
|
+ return true;
|
|
|
+ } catch (error) {
|
|
|
+ console.error(`FFmpeg execution failed with error: ${error instanceof Error ? error.message : String(error)}`);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
}
|
|
|
/**
|
|
|
* 从视频文件中提取封面 提前视频第5帧的封面
|
|
|
@@ -1589,7 +1623,17 @@ async function getFFmpegCover(inputPath: string, outputPath: string) {
|
|
|
*/
|
|
|
async function getVideoFFmpegCover(inputPath: string, outputPath: string) {
|
|
|
|
|
|
- let commands = ["ffmpeg", "-y","-i", inputPath, "-ss", "00:00:00", "-t", "1","-r",'1','-q:v','2','-f','image2', outputPath];
|
|
|
+ // 更可靠的命令,专门提取视频第一帧作为封面
|
|
|
+ const commands = [
|
|
|
+ "ffmpeg",
|
|
|
+ "-y", // 覆盖输出文件(如果存在)
|
|
|
+ "-i", inputPath, // 输入文件
|
|
|
+ "-ss", "00:00:00", // 定位到开始位置
|
|
|
+ "-vframes", "1", // 只提取1帧
|
|
|
+ "-q:v", "2", // 高质量JPEG(1-31,2是最高质量)
|
|
|
+ "-f", "image2", // 强制输出为图像格式
|
|
|
+ outputPath
|
|
|
+ ];
|
|
|
FFmpeg.execute(commands, {
|
|
|
logCallback: (logLevel: number, logMessage: string) => {
|
|
|
console.info(`[FFmpegX LOG] [${logLevel}]${logMessage}`)
|