Determine file type in Ruby

后端 未结 13 1084
耶瑟儿~
耶瑟儿~ 2020-11-30 22:21

How does one reliably determine a file\'s type? File extension analysis is not acceptable. There must be a rubyesque tool similar to the UNIX file(1) comman

13条回答
  •  星月不相逢
    2020-11-30 22:54

    I found shelling out to be the most reliable. For compatibility on both Mac OS X and Ubuntu Linux I used:

    file --mime -b myvideo.mp4
    video/mp4; charset=binary

    Ubuntu also prints video codec information if it can which is pretty cool:

    file -b myvideo.mp4
    ISO Media, MPEG v4 system, version 2

提交回复
热议问题