AI の力でファイル形式を判定する Google 製の「Magika」を試してみる
面白そうだったので試しに使ってみました。
検証したバージョン
- Magika v0.5.0
Magika の使い方
インストール
pip でインストールできます。
$ pip install magika
CLI として使う
単一のファイルの形式を判定する
magika にファイル名を渡して実行すると、そのファイルの形式を判定してくれます。
$ magika <ファイル名>
例えば、次のようなファイルを main.go という名前で作成します。
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
このファイルの形式を判定してみます。
$ magika ./main.go
main.go: Golang source (code)
おお〜。
複数のファイルの形式を判定する
同時に複数のファイルを指定することもできます。
$ magika ./main.go ./main.js
main.go: Golang source (code)
main.js: JavaScript source (code)
おお〜。
-r, --recursive フラグを使うと、指定したディレクトリを再帰的に探索してファイル形式を判定することができます。
.
├── main.go
├── main.js
└── subdir
└── main.py
$ magika --recursive ./
main.go: Golang source (code)
main.js: JavaScript source (code)
subdir/main.py: Python source (code)
おお〜。
判定するファイルを標準入力から読み取る
標準入力から読み取ることもできます。
この場合、 magika の引数には - を指定する必要があります。
$ cat ./main.go | magika -
-: Golang source (code)
おお〜。
JSON 形式で出力する
--json フラグをつけると、スコアなど様々な情報を含めた JSON 形式で出力してくれます。
$ magika ./main.go --json
[
{
"path": "main.go",
"dl": {
"ct_label": "go",
"score": 0.9999659061431885,
"group": "code",
"mime_type": "text/x-golang",
"magic": "ASCII text",
"description": "Golang source"
},
"output": {
"ct_label": "go",
"score": 0.9999659061431885,
"group": "code",
"mime_type": "text/x-golang",
"magic": "ASCII text",
"description": "Golang source"
}
}
]
おお〜。
他にもいくつかの表示オプションがあります。
詳しくはヘルプをご参照ください。
$ magika --help
Python ライブラリとして使う
単一のファイルの形式を判定する
Magika オブジェクトの identify_path メソッドに Path オブジェクトを渡すことで、単一のファイルの形式を判定できます。
from magika import Magika
from pathlib import Path
m = Magika()
# 単一のファイルを指定
response = m.identify_path(Path("./main.go"))
print(response)
MagikaResult オブジェクトが返ってきます。
MagikaResult(path='main.go', dl=ModelOutputFields(ct_label='go', score=0.9999659061431885, group='code', mime_type='text/x-golang', magic='ASCII text', description='Golang source'), output=MagikaOutputFields(ct_label='go', score=0.9999659061431885, group='code', mime_type='text/x-golang', magic='ASCII text', description='Golang source'))
おお〜。
複数のファイルの形式を判定する
複数のファイルの形式を判定するためには identify_paths メソッドを使います。
from magika import Magika
from pathlib import Path
m = Magika()
# 複数のファイルを指定
response = m.identify_paths([Path("./main.go"), Path("./main.js")])
print(response)
MagikaResult オブジェクトのリストが返ってきます。
[MagikaResult(path='main.go', dl=ModelOutputFields(ct_label='go', score=0.9999659061431885, group='code', mime_type='text/x-golang', magic='ASCII text', description='Golang source'), output=MagikaOutputFields(ct_label='go', score=0.9999659061431885, group='code', mime_type='text/x-golang', magic='ASCII text', description='Golang source')), MagikaResult(path='main.js', dl=ModelOutputFields(ct_label='javascript', score=0.9996428489685059, group='code', mime_type='application/javascript', magic='JavaScript source', description='JavaScript source'), output=MagikaOutputFields(ct_label='javascript', score=0.9996428489685059, group='code', mime_type='application/javascript', magic='JavaScript source', description='JavaScript source'))]
おお〜。
バイト列を渡して判定する
identify_bytes メソッドを使うとバイト列を渡してファイル形式を判定できます。
from magika import Magika
m = Magika()
# バイト列を指定
response = m.identify_bytes(b"console.log('Hello, World!');")
print(response)
MagikaResult(path='-', dl=ModelOutputFields(ct_label='javascript', score=0.9996150732040405, group='code', mime_type='application/javascript', magic='JavaScript source', description='JavaScript source'), output=MagikaOutputFields(ct_label='javascript', score=0.9996150732040405, group='code', mime_type='application/javascript', magic='JavaScript source', description='JavaScript source'))
おお〜。
なお、大きなファイルを扱う場合は identify_path か identify_bytes を使った方が基本的にはいいみたいです。
If you are dealing with big files, the identify_path and identify_paths variants are generally better: their implementation seek() around the file to extract the needed features, without loading the entire content in memory.
JavaScript ライブラリとして使う
ここでは具体的な使い方は紹介しませんが、一応 JavaScript ライブラリもあります。
こちらはデモページ用に開発されたとのことです。
Magika がサポートしているファイル形式
--list-output-content-types フラグをつけて実行すると Magika がサポートしているファイル形式の一覧を確認できます。
$ magika --list-output-content-types
もしくは magika/docs/supported-content-types-list.md をご参照ください。
ファイル形式一覧
| Index | Content Type Label | MIME Type | Description |
|---|---|---|---|
| 1 | ai | application/pdf | Adobe Illustrator Artwork |
| 2 | apk | application/vnd.android.package-archive | Android package |
| 3 | appleplist | application/x-plist | Apple property list |
| 4 | asm | text/x-asm | Assembly |
| 5 | asp | text/html | ASP source |
| 6 | batch | text/x-msdos-batch | DOS batch file |
| 7 | bmp | image/bmp | BMP image data |
| 8 | bzip | application/x-bzip2 | bzip2 compressed data |
| 9 | c | text/x-c | C source |
| 10 | cab | application/vnd.ms-cab-compressed | Microsoft Cabinet archive data |
| 11 | cat | application/octet-stream | Windows Catalog file |
| 12 | chm | application/chm | MS Windows HtmlHelp Data |
| 13 | coff | application/x-coff | Intel 80386 COFF |
| 14 | crx | application/x-chrome-extension | Google Chrome extension |
| 15 | cs | text/plain | C# source |
| 16 | css | text/css | CSS source |
| 17 | csv | text/csv | CSV document |
| 18 | deb | application/vnd.debian.binary-package | Debian binary package |
| 19 | dex | application/x-android-dex | Dalvik dex file |
| 20 | directory | inode/directory | A directory |
| 21 | dmg | application/x-apple-diskimage | Apple disk image |
| 22 | doc | application/msword | Microsoft Word CDF document |
| 23 | docx | application/vnd.openxmlformats-officedocument.wordprocessingml.document | Microsoft Word 2007+ document |
| 24 | elf | application/x-executable-elf | ELF executable |
| 25 | emf | application/octet-stream | Windows Enhanced Metafile image data |
| 26 | eml | message/rfc822 | RFC 822 mail |
| 27 | empty | inode/x-empty | Empty file |
| 28 | epub | application/epub+zip | EPUB document |
| 29 | flac | audio/flac | FLAC audio bitstream data |
| 30 | gif | image/gif | GIF image data |
| 31 | go | text/x-golang | Golang source |
| 32 | gzip | application/gzip | gzip compressed data |
| 33 | hlp | application/winhlp | MS Windows help |
| 34 | html | text/html | HTML document |
| 35 | ico | image/vnd.microsoft.icon | MS Windows icon resource |
| 36 | ini | text/plain | INI configuration file |
| 37 | internetshortcut | application/x-mswinurl | MS Windows Internet shortcut |
| 38 | iso | application/x-iso9660-image | ISO 9660 CD-ROM filesystem data |
| 39 | jar | application/java-archive | Java archive data (JAR) |
| 40 | java | text/x-java | Java source |
| 41 | javabytecode | application/x-java-applet | Java compiled bytecode |
| 42 | javascript | application/javascript | JavaScript source |
| 43 | jpeg | image/jpeg | JPEG image data |
| 44 | json | application/json | JSON document |
| 45 | latex | text/x-tex | LaTeX document |
| 46 | lisp | text/x-lisp | Lisp source |
| 47 | lnk | application/x-ms-shortcut | MS Windows shortcut |
| 48 | m3u | text/plain | M3U playlist |
| 49 | macho | application/x-mach-o | Mach-O executable |
| 50 | makefile | text/x-makefile | Makefile source |
| 51 | markdown | text/markdown | Markdown document |
| 52 | mht | application/x-mimearchive | MHTML document |
| 53 | mp3 | audio/mpeg | MP3 media file |
| 54 | mp4 | video/mp4 | MP4 media file |
| 55 | mscompress | application/x-ms-compress-szdd | MS Compress archive data |
| 56 | msi | application/x-msi | Microsoft Installer file |
| 57 | mum | text/xml | Windows Update Package file |
| 58 | odex | application/x-executable-elf | ODEX ELF executable |
| 59 | odp | application/vnd.oasis.opendocument.presentation | OpenDocument Presentation |
| 60 | ods | application/vnd.oasis.opendocument.spreadsheet | OpenDocument Spreadsheet |
| 61 | odt | application/vnd.oasis.opendocument.text | OpenDocument Text |
| 62 | ogg | audio/ogg | Ogg data |
| 63 | outlook | application/vnd.ms-outlook | MS Outlook Message |
| 64 | pcap | application/vnd.tcpdump.pcap | pcap capture file |
| 65 | application/pdf | PDF document | |
| 66 | pebin | application/x-dosexec | PE executable |
| 67 | pem | application/x-pem-file | PEM certificate |
| 68 | perl | text/x-perl | Perl source |
| 69 | php | text/x-php | PHP source |
| 70 | png | image/png | PNG image data |
| 71 | postscript | application/postscript | PostScript document |
| 72 | powershell | application/x-powershell | Powershell source |
| 73 | ppt | application/vnd.ms-powerpoint | Microsoft PowerPoint CDF document |
| 74 | pptx | application/vnd.openxmlformats-officedocument.presentationml.presentation | Microsoft PowerPoint 2007+ document |
| 75 | python | text/x-python | Python source |
| 76 | pythonbytecode | application/x-bytecode.python | Python compiled bytecode |
| 77 | rar | application/x-rar | RAR archive data |
| 78 | rdf | application/rdf+xml | Resource Description Framework document (RDF) |
| 79 | rpm | application/x-rpm | RedHat Package Manager archive (RPM) |
| 80 | rst | text/x-rst | ReStructuredText document |
| 81 | rtf | text/rtf | Rich Text Format document |
| 82 | ruby | application/x-ruby | Ruby source |
| 83 | rust | application/x-rust | Rust source |
| 84 | scala | application/x-scala | Scala source |
| 85 | sevenzip | application/x-7z-compressed | 7-zip archive data |
| 86 | shell | text/x-shellscript | Shell script |
| 87 | smali | application/x-smali | Smali source |
| 88 | sql | application/x-sql | SQL source |
| 89 | squashfs | application/octet-stream | Squash filesystem |
| 90 | svg | image/svg+xml | SVG Scalable Vector Graphics image data |
| 91 | swf | application/x-shockwave-flash | Macromedia Flash data |
| 92 | symlink | inode/symlink | Symbolic link to <path> |
| 93 | symlinktext | text/plain | Symbolic link (textual representation) |
| 94 | tar | application/x-tar | POSIX tar archive |
| 95 | tga | image/x-tga | Targa image data |
| 96 | tiff | image/tiff | TIFF image data |
| 97 | torrent | application/x-bittorrent | BitTorrent file |
| 98 | ttf | font/sfnt | TrueType Font data |
| 99 | txt | text/plain | Generic text document |
| 100 | unknown | application/octet-stream | Unknown binary data |
| 101 | vba | text/vbscript | MS Visual Basic source (VBA) |
| 102 | wav | audio/x-wav | Waveform Audio file (WAV) |
| 103 | webm | video/webm | WebM data |
| 104 | webp | image/webp | WebP data |
| 105 | winregistry | text/x-ms-regedit | Windows Registry text |
| 106 | wmf | image/wmf | Windows metafile |
| 107 | xar | application/x-xar | XAR archive compressed data |
| 108 | xls | application/vnd.ms-excel | Microsoft Excel CDF document |
| 109 | xlsb | application/vnd.openxmlformats-officedocument.spreadsheetml.sheet | Microsoft Excel 2007+ document (binary format) |
| 110 | xlsx | application/vnd.openxmlformats-officedocument.spreadsheetml.sheet | Microsoft Excel 2007+ document |
| 111 | xml | text/xml | XML document |
| 112 | xpi | application/zip | Compressed installation archive (XPI) |
| 113 | xz | application/x-xz | XZ compressed data |
| 114 | yaml | application/x-yaml | YAML source |
| 115 | zip | application/zip | Zip archive data |
| 116 | zlibstream | application/zlib | zlib compressed data |
まとめ
AI すごい。
Discussion