Home » Questions » Computers [ Ask a new question ]

How do I find the codec for an AVI?

How do I find the codec for an AVI?

How can I find which codec an AVI file needs? A method that does not require downloading a program would be preferred (such as looking at the file in notepad).

Asked by: Guest | Views: 293
Total answers/comments: 2
bert [Entry]

"Open in notepad, do a search for 'vids' - the FOURCC value is right after that. Eg you'll find something like:

vidsWMV1

Then google for 'fourcc WMV1' in this example."
bert [Entry]

"If you have a Unix-like shell, you probably have a program called 'strings' which finds sequences of printable characters. By default, the minimum length is four, which is fine for this need. (It can be changed on the command line.) The 'vids' and the fourcc will be consecutive in the output (but not one string in my AVI).

You could pipe the result into something simple like 'more' and find it yourself. Or you could pipe it into head and hope to guess the number of lines you will need. Or you could be a little more sophisticated and pipe it through sed and have it print the line that matches the desired pattern. Or you can use awk to do the same. On my cygwin bash shell, those options looks like these commands:

strings foo.avi | more
strings foo.avi | head -10
strings foo.avi | sed -n '/vids/,+1p'
strings foo.avi | awk 'NR==1,/vids/{if( $0 == ""vids"" ) { getline ; print ; exit } } '

The disadvantage to the third version is that I have sed process the entire file, so it might take a while to complete. I'm confident that there is a way to make it stop after the first match, but I'm not sufficiently expert with sed to know (or find) that. The awk version does explicitly stop if the condition is true (get next line, print that, exit -- whereas the sed command has only the first two of those with +1 and p). In both the sed and awk command, the /vids/ is the regular expression we need (i.e. the search string in Chris' answer above). Also, the awk construction 'NR==1,/vids/' says to apply the following code block to the lines from number 1 (first input line) to wherever it finds that regular expression (matching string). The if condition prevents printing all the lines up to the one that matches. I have it match the full line ($0), which for the strings command, should work fine; strings are printed one per line anyway.

Note that the string it finds may well include extra characters (e.g. from my AVI file it finds 'strf(' not 'strf' which is the fourcc). If you really need to chop it at four characters (e.g. for a script that needs to match it against a standard list), you can pipe the answer to 'head -c 4' and get just the fourcc. Here's my final answer:

strings foo.avi | awk 'NR==1,/vids/{if( $0 == ""vids"" ) { getline ; print ; exit } } ' | head -c 4

For what it's worth, Windows Media Player listed the codec in my AVI as ""-"" for both audio and video, but the above worked as expected."