Code snippets, tech tricks and other bits and bobs

Video MIME types

So, we're all gee'ed up with HTML5 video and it's all working smoothly, so clients are going to want to start updating their videos themselves.

Grand. But trying to explain to users how to convert their videos into 3 different formats with the correct encoding settings is not a task to be taken lightly. I've dealt with people who work with digital video every day, and they still don't grasp it. And for many, they simply don't want to - they've got other things to do.

As a result I'm working on integrating video transcoding services into my CMS. This has all been fairly painless to get working, but now I come to think about ensuring that malicious files are not uploaded to the server. Now, as long as these files can be ensured to never be executed on your server, only sent to the encoding server for conversion, you can let them worrry about all of that - but if you can't ensure that, or are just particularly paranoid, you might want to do some heavy checking when receiving the files.

Zencoder, which I am using currently, reckon they can convert pretty much anything, and users being users, they are bound to decide to try and send you some rare file format.

With this in mind I've compiled a list of video extensions (which are NOT to be relied on as an indicator of a valid file type), and video MIME types.
Checking MIME types can be tricky - not all server setups support MIME checking - and many video formats don't have an associated MIME type, instead reporting as 'application/octet-stream'. But you can certainly exclude the upload of any files with a MIME type not on your list.

So I have compiled three lists, none of them should be considered completely exhaustive, but it took some effort to compile these from a variety of sources, so it may be a useful resource. There are some MIMEs I don't have extensions listed for, and vice versa (partcularly where an extension doesn't have a type), as well as types which may be missing valid extensions but there is a limit to how much effort it was worth putting into it.

Errata and addendum are most welcome!

Video MIME types and associated extensions

"video/fli","fli,flc"
"video/flc","flc,vfw"
"video/flv","flv"
"video/gl","gl"
"video/mp4","mp4,mpg4"
"video/mpeg","m1v,m2v,mp2,mp3,mpa,mpe,mpeg,mpegv,mpg,mpv,vbs,rmvb"
"video/mpeg-system","mpg,mpeg,mpe,vob"
"video/msvideo","avi,vfw"
"video/ogg","ogg"
"video/quicktime","mov,moov,qt,mp4,qtm,qts"
"video/sd-video","sdv"
"video/ty","tivo"
"video/vc1","vc1"
"video/vdo","vdo"
"video/vivo","viv,vivo"
"video/vnd.divx","divx"
"video/vnd.rn-realvideo","rv"
"video/vnd.vivo","viv"
"video/vosaic","vos"
"video/vp3","vp3"
"video/wavelet","wv"
"video/webm","webm,vp8"
"video/wtv","wtv"
"video/x-amt-demorun","xdr"
"video/x-amt-showrun","xsr"
"video/x-atomic3d-feature","fmf"
"video/x-divx","divx"
"video/x-dl","dl"
"video/x-dv","dif,dv"
"video/x-fli","fli,flc"
"video/x-flv","flv"
"video/x-gl","gl"
"video/x-isvideo","isu"
"video/x-m4v","m4v"
"video/x-matroska","mkv"
"video/x-motion-jpeg","mjpg"
"video/x-mpb","mpb"
"video/x-mpeg","m2ts,mp2,mp3,mpe,mpeg,mpegv,mpg,mpv,mts"
"video/x-mpeg-system","mpg,mpeg,mpe,vob"
"video/x-mpeg2","mp2,mpv2,mp2v,mp2ve"
"video/x-mpeq2a","mp2"
"video/x-ms-asf","asf,asx"
"video/x-ms-asf-plugin","asf,asx,wmv"
"video/x-msvideo","avi,asf,vfw,wmv"
"video/x-ms-wm","wm,wmv"
"video/x-ms-wmp","wmv,wmp"
"video/x-ms-wm","wm"
"video/x-ms-wmv","wmv"
"video/x-ms-wmx","wmx"
"video/x-ms-wvx","wvx,wmv"
"video/x-mve","mve"
"video/x-ogg","ogm,ogv"
"video/x-pva","pva"
"video/x-qtc","qtc"
"video/x-quicktime","mov,moov,qt,mp4,qtm,qts"
"video/x-raw-yuv","yuv"
"video/x-scm","scm"
"video/x-sgi-movie","movie,mv"
"video/x-videogram","vgm,vgx,xdr"
"video/x-videogram-plugin","vgp"
"video/x-vp3","vp3"
"video/x-vp6","vp6"
"video/x-vp7","vp7"
"video/x-wmv","wmv"
"video/x-xvid","xvid"

Video MIME types

application/mp21
application/mp4
application/mpeg4-iod
application/mpeg4-muxcodetable
application/mxf
application/ogg
application/octet-stream
application/vnd.ms-asf
application/vnd.rn-realmedia
application/vnd.rn-realmedia-vbr
application/vnd.rn-realplayer
application/x-nsv-vp3-mp3
application/x-mplayer2
application/x-ms-wms
application/x-ms-wmp
application/x-nsv-vp3-mp3
application/x-ogg
application/x-troff-msvideo
application/x-winamp
application/x-shockwave-flash
audio/x-m4a
audio/x-pn-realaudio
audio/x-pn-realaudio-plugin
audio/x-pn-realvideo
audio/x-realaudio
video/3gpp
video/3gpp2
video/animaflex
video/annodex
video/avi
video/avs-video
video/divx
video/dl
video/fli
video/flc
video/gl
video/gvi
video/h264
video/m75
video/mj2
video/mp4
video/mp4v-es
video/mpeg
video/mpeg4
video/mpeg4-generic
video/msvideo
video/nv
video/ogg
video/quicktime
video/sd-video
video/ty
video/vc1
video/vdo
video/vivo
video/vnd.avi
video/vnd.divx
video/vnd.rn-realvideo
video/vnd.vivo
video/vosaic
video/vp3
video/wavelet
video/webm
video/wtv
video/x-3ivx
video/x-ajp
video/x-amt-demorun
video/x-amt-showrun
video/x-amv
video/x-atomic3d-feature
video/x-divx
video/x-dl
video/x-dv
video/x-ffv
video/x-fli
video/x-flv
video/x-gl
video/x-h263
video/x-h264
video/x-huffyuv
video/x-indeo
video/x-intel-h263
video/x-isvideo
video/x-ivf
video/x-jpeg
video/x-la-asf
video/x-m4v
video/x-matroska
video/x-motion-jpeg
video/x-mpeg
video/x-mpeg2
video/x-mpeq2a
video/x-ms-asf
video/x-ms-asf-plugin
video/x-ms-wm
video/x-ms-wmx
video/x-ms-wmv
video/x-ms-wvx
video/x-msmpeg
video/x-msvideo
video/x-msvideocodec
video/x-ogg
video/x-pn-multirate-realvideo
video/x-pn-realvideo
 video/x-pva
video/x-qtc
video/x-quicktime
video/x-raw-rgb
video/x-raw-yuv
video/x-rle
video/x-scm
video/x-sgi-movie
video/x-svq
video/x-tarkin
video/x-theora
video/x-videogram
video/x-videogram-plugin
video/x-vp3
video/x-vp6
video/x-vp7
video/x-wmv
video/x-xvid

Video file extensions

3gp
3g2
3gp2
3gpp
3gpp2
3p2
afl
ajp
amv
asf
asx
avi
avs
axv
bdm
bdmv
dif
divx
dl
dv
dv-avi
dvx
f4v
fli
flc
flv
fmf
gl
gvi
hdmov
hdv
isu
ivf
ivr
ls4
k3g
m15
m1s
m1v
m21
m2t
m2ts
m2p
m2s
m2v
m4e
m4v
m75
mgv
mj2
mjp
mjpg
mkv
mnv
mod
moov
mov
movie
mp2
mp21
mp2v
mp3
mp4
mpa
mpe
mpeg
mpg
mpg4
mpv
mpv2
mpv2e
mqv
mts
mtv
mv
mxf
nsv
ogg
ogv
pmf
pva
pvr
pxv
qt
qtc
qtm
r3d
rec
rm
rmd
rmvb
rp
rts
rv
scm
tivo
tod
tp0
tpd
vbs
vc1
vdo
vgm
vgp
vgx
vfw
vid
viv
vivo
vob
vos
vp3
vro
webm
wm
wmp
wmv
wtv
wv
wmx
wvx
xdr
xsr
yu

Filed under  //   MIME Types   Video  

PHP mime_content_type() replacement

mime_content_type is not always available depending on your server There are various ways to get around it. The current PHP recommendation is to use the Fileinfo (aka finfo) library

An useful alternative is to use exec()

If you know (or are only expecting) that your file is an image you can also use getimagesize(), but this is limited to the image types getimagesize() recognises.]

My preference is the following combination of all three methods, with ‘application/octet-stream’ as a fallback for anything you can’t recognise:

[update 201105302125]

Function updated with more error reporting, including additional debug info if named constant __ADMIN_DEBUG has been set

[/update]

if(!function_exists('mime_content_type')) {
    function mime_content_type($filename){
        if(!is_file($filename)){
            $error = 'Error: File not found';
            if(defined('__ADMIN_DEBUG')){
                $error .= ' mime_content_type(): ' . $filename;
            }
            return $error;
        }elseif(function_exists('finfo_open')) {
                $finfo = @finfo_open(FILEINFO_MIME_TYPE);
                if (!$finfo) {
                    $error = 'Error: Unable to verify MIME content type';
                    if(defined('__ADMIN_DEBUG')){
                        $error .= ' finfo(): Opening magic.mime database may have failed';
                    }
                    return $error;
                }
                if($mimetype = @finfo_file($finfo, $filename)){
                    finfo_close($finfo);
                    return $mimetype;
                }else{
                    $error = 'Error: Unable to verify MIME content type';
                    if(defined('__ADMIN_DEBUG')){
                        $error .= ' Error executing finfo() for ' . $filename;
                    }
                    return $error;
                }
        }elseif(function_exists('exec') && function_exists('escapeshellarg')){
            if($execmime = trim(@exec('file -bi ' . @escapeshellarg($filename)))){
                return $execmime;
            }else{
                $error = 'Error: Unable to verify MIME content type';
                if(defined('__ADMIN_DEBUG')){
                    $error .= ' exec()/escapeshellarg() can not open ' . $filename;
                }
                return $error;
            }
        }elseif(function_exists('pathinfo')){
            if($pathinfo = @pathinfo($filename)){
                $imagetypes = array('gif', 'jpeg', 'jpg', 'png', 'swf', 'psd', 'bmp', 'tiff', 'tif', 'jpc', 'jp2', 'jpx', 'jb2', 'swc', 'iff', 'wbmp', 'xbm', 'ico' );
                if(in_array($pathinfo['extension'], $imagetypes) && getimagesize($file)){
                    $size = getimagesize($filename);
                    return $size['mime'];
                }
            }else{
                $error = 'Error: Unable to verify MIME content type';
                if(defined('__ADMIN_DEBUG')){
                    $error .= ' pathinfo() can not open ' . $filename;
                }
                return $error;
            }
        }else{
            return 'application/octet-stream';
        }
    }
}

Filed under  //   MIME Types   PHP