Skip to content

Commit

Permalink
enhance(backend): ビデオファイルにビデオトラックがあるかを確認するように
Browse files Browse the repository at this point in the history
  • Loading branch information
kakkokari-gtyih committed Mar 12, 2024
1 parent 7943e59 commit 23d38a2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
41 changes: 41 additions & 0 deletions packages/backend/src/core/FileInfoService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,27 @@ export class FileInfoService {
return mime;
}

/**
* ビデオファイルにビデオトラックがあるかどうかチェック
* (ない場合:m4a, webmなど)
*/
@bindThis
private hasVideoTrackOnVideoFile(path: string): Promise<boolean> {
return new Promise((resolve, reject) => {
try {
FFmpeg.ffprobe(path, (err, metadata) => {
if (err) {
resolve(true);
return;
}
resolve(metadata.streams.some((stream) => stream.codec_type === 'video'));
});
} catch (e) {
resolve(true);
}
});
}

/**
* Detect MIME Type and extension
*/
Expand All @@ -339,6 +360,26 @@ export class FileInfoService {
return TYPE_SVG;
}

if (type.mime.startsWith('video') && !(await this.hasVideoTrackOnVideoFile(path))) {
const newMime = `audio/${type.mime.split('/')[1]}`;
if (newMime === 'audio/mp4') {
return {
mime: 'audio/aac',
ext: 'm4a',
};
}
if (newMime === 'audio/webm') {
return {
mime: 'audio/webm',
ext: 'webm',
};
}
return {
mime: newMime,
ext: type.ext,
};
}

return {
mime: this.fixMime(type.mime),
ext: type.ext,
Expand Down
3 changes: 0 additions & 3 deletions packages/backend/test/unit/FileInfoService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,6 @@ describe('FileInfoService', () => {
});
});

/*
* video/webmとして検出されてしまう
test('WEBM AUDIO', async () => {
const path = `${resources}/kick_gaba7.webm`;
const info = await fileInfoService.getFileInfo(path, { skipSensitiveDetection: true }) as any;
Expand All @@ -344,6 +342,5 @@ describe('FileInfoService', () => {
},
});
});
*/
});
});

0 comments on commit 23d38a2

Please sign in to comment.