bash 使用 linux 'file' 命令确定类型(即图像、音频或视频)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8101812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:11:35  来源:igfitidea点击:

Using the linux 'file' command to determine type (ie. image, audio, or video)

bashshellfile-type

提问by puk

The word filehere refers to the shell filecommand, and not actual files. I want to determine whether a file is a, for example, video file (.mpg, .mkv, .avi). fileis pretty good at returning imagefor image files, videofor video files, and audiofor audio files (and application/x-emptyfor some reason for text). My question is how reliable this is for identifying types. If I did a simple

file这里的词指的是 shell文件命令,而不是实际的文件。我想确定一个文件是否是例如视频文件 ( .mpg, .mkv, .avi)。file非常擅长返回image图像文件、video视频文件和audio音频文件(以及application/x-empty出于某种原因的文本)。我的问题是这对于识别类型有多可靠。如果我做了一个简单的

file -ib deliverance.avi | grep video

would that work for all of the mainvideo files outlined here?

这对这里列出的所有主要视频文件都有效吗?

回答by John Flatness

The results from fileare less than perfect, and it has more problems with some types of files than others. File basically just looks for particular pieces of binary data in predictable patterns to figure out filetypes.

来自的结果file并不完美,并且某些类型的文件比其他文件存在更多问题。File 基本上只是以可预测的模式查找特定的二进制数据片段以找出文件类型。

Unfortunately, in particular, some of the filetypes often used for video fall into this "problematic" category. The newer container formats like .mp4and .mkvusually have several different MIME types that should properly depend on what type of data is being contained. For example, an .mp4could properly be identified as video/mp4, audio/mp4, or application/mp4depending on the content.

不幸的是,特别是一些经常用于视频的文件类型属于这种“有问题”的类别。较新的容器格式喜欢.mp4并且.mkv通常有几种不同的 MIME 类型,它们应该正确地取决于所包含的数据类型。例如,.mp4可以适当地确定为video/mp4audio/mp4,或 application/mp4取决于内容。

In practice, fileoften makes guesses that simply conform with common usage, and it may work perfectly well for you. For example, while I mentioned some theoretical difficulties with identifying Matroska files correctly, filebasically just assumes that any Matroska file is a video. On the other hand, the usage of the Ogg container is more evenly split between audio and video, and I believe the current version of filejust splits the difference, and identifies Ogg files as application/ogg, which wouldn't fall into any of your categories.

在实践中,file经常做出简单符合常见用法的猜测,它可能对您非常有效。例如,虽然我提到了正确识别 Matroska 文件的一些理论上的困难,但file基本上只是假设任何 Matroska 文件都是视频。另一方面,Ogg 容器的使用在音频和视频之间更加平均地划分,我相信当前版本的file只是划分差异,并将 Ogg 文件标识为application/ogg,这不会属于您的任何类别。

The one thing I can say with certainty is that you want the most up-to-date version of fileyou can get your hands on. The "magic" files that contain the patterns to match against and the MIME types that will result from a match are updated fairly often to include newer filetypes like WebM, or just to improve accuracy for older types.

我可以肯定地说的一件事是,您想要最新的版本file。包含要匹配的模式和匹配结果的 MIME 类型的“魔法”文件会经常更新以包含较新的文件类型,例如 WebM,或者只是为了提高旧类型的准确性。

回答by frankc

file works by referencing the header of the file against a "magic number" file. I suspect the best way to see how robust file is to check your local magic number file (possibly /usr/share/magic but see man file for details) for the file types from your referenced list.

file 的工作原理是根据“幻数”文件引用文件头。我怀疑查看文件有多健壮的最佳方法是检查您的本地幻数文件(可能是 /usr/share/magic,但有关详细信息,请参阅 man 文件)以获取引用列表中的文件类型。

回答by Shalmezad

It seems like it should work for most video/audio/image files. But, if it doesn't, there's actually a file that contains the relations between an extension and it's type:

它似乎适用于大多数视频/音频/图像文件。但是,如果没有,实际上有一个文件包含扩展名与其类型之间的关系:

The information identifying these files is read from the compiled magic file /usr/share/magic.mgc , or /usr/share/magic if the compile file does not exist.

识别这些文件的信息是从编译后的魔法文件 /usr/share/magic.mgc 中读取的,如果编译文件不存在,则从 /usr/share/magic 中读取。

see: http://linux.about.com/library/cmd/blcmdl1_file.htm

见:http: //linux.about.com/library/cmd/blcmdl1_file.htm

Hope this helps!

希望这可以帮助!