2

Ive got a folder with video files which are a few different file extensions. eg : .m4v, .avi, .mp4, .mkv

I deleted all the extensions off the file names using a batch renaming tool, but when ive done this all the files now show up a unix executable files. (im on a mac so i think this is just the default).

I have a couple of hundred files like this, is there any way i can batch review the files, ascertain the correct file extension and set the correct file extension type for each file.

  • AVI may be detecred by RIFF (offset 0) + AVI (offset 8) signatures. MP4 may be detected by ftypmmp4 (offset 4) signature. M4V may be detected by mtypM4V (offset 4) signature. But batch processing cannot do this. – Akina Dec 26 '19 at 16:16
  • On macOS the file command will identify .m4v, .avi and .mp4. – lx07 Dec 26 '19 at 18:13
  • ffmpeg will output data about a file you pass with the -i option, I renamed an .mkv file to just test and did this ffmpeg -i test 2>&1 | grep encoder and got encoder : libebml v1.3.6 + libmatroska v1.4.9 – Michael Harvey Dec 26 '19 at 18:34
1

In macOS 10.14 Mojave the file command correctly identifies .m4v, .avi and .mp4 from the file contents. Note the version of file on macOS is outdated compared to the current version and so some other filetypes may not be identified correctly. For example a test mkv downloaded from www.sample-videos.com was not identified.

For filetypes that file can correctly identify you can append the relevant suffix by identifying the file and then renaming it.

file * | grep <FILETYPE> | sed s'<SOMETHING>' | xargs -I % mv % %<.EXTENSION>

Filetypes identified

I downloaded some sample files from here and there, ranamed them file_example_something and removed the extensions. From that directory you can see everything except MKV is identified and has filetype in the descrition provided by file command:

A1398% file file_example_*
file_example_AVI_480_750kB:         RIFF (little-endian) data, AVI, 480 x 270, 30.00 fps, video: H.264 X.264 or H.264, audio: (stereo, 48000 Hz)
file_example_AVI_480_750kB copy:    RIFF (little-endian) data, AVI, 480 x 270, 30.00 fps, video: H.264 X.264 or H.264, audio: (stereo, 48000 Hz)
file_example_M4V_720:               ISO Media, Apple iTunes Video (.M4V) Video
file_example_M4V_720 copy:          ISO Media, Apple iTunes Video (.M4V) Video
file_example_MKV_1280x720_1mb:      , created: Thu Jul 11 00:22:26 1974, modified: Sun Mar 26 05:53:06 2028
file_example_MKV_1280x720_1mb copy: , created: Thu Jul 11 00:22:26 1974, modified: Sun Mar 26 05:53:06 2028
file_example_MP4_480_1_5MG:         ISO Media, MP4 v2 [ISO 14496-14]
file_example_MP4_480_1_5MG copy:    ISO Media, MP4 v2 [ISO 14496-14]

You can therefore extract the file names of a certain type if you make a couple of assumptions (for example you don't have : in a file name and you don't have .M4V files with .avi in the file name)

Example for .m4v

For example, this uses grep (to select a file type from find results description) and then sed to remove everything after the first :.

A1398% file * | grep .M4V | sed s'/:.*[^:]*$//'
file_example_M4V_720
file_example_M4V_720 copy

If the results look reasonable you can then rename them - in this case only M4V

M4V Correction

file * | grep .M4V | sed s'/:.*[^:]*$//' | xargs -I % mv % %.m4v

Results

A1398% ls -la file_example_M4V*
-rw-r--r--@ 1 hali  staff  3538130 Dec 26 18:31 file_example_M4V_720
-rw-r--r--@ 1 hali  staff  3538130 Dec 26 18:31 file_example_M4V_720 copy
A1398%
A1398% file * | grep .M4V | sed s'/:.*[^:]*$//' | xargs -I % mv % %.m4v
A1398% ls -la file_example_M4V*
-rw-r--r--@ 1 hali  staff  3538130 Dec 26 18:31 file_example_M4V_720 copy.m4v
-rw-r--r--@ 1 hali  staff  3538130 Dec 26 18:31 file_example_M4V_720.m4v

Therefore for identifiable filetypes in your question:

file * | grep .M4V | sed s'/:.*[^:]*$//' | xargs -I % mv % %.m4v
file * | grep .MP4 | sed s'/:.*[^:]*$//' | xargs -I % mv % %.mp4
file * | grep .AVI | sed s'/:.*[^:]*$//' | xargs -I % mv % %.avi
| improve this answer | |
  • Thanks so much, i updated the question last night to also include .mkv files would the above work for them also ? – sam Dec 27 '19 at 12:10
  • I tested a mkv downloaded from the internet and file did not identify it (you can see results in the answer - it just shows garbage creation data). On macOS the definitions are in /usr/share/file/magic with video definitions seemingly in the files animation and riff. There is nothing there for mkv and neither is there in latest source on github. mkv is not a video format anyway - it is a container that can contain anything (eg avi files). If you get rid of the other types you can just try renaming the rest mkv... – lx07 Dec 27 '19 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.