Skip to content

Commit

Permalink
Fix isom ftyp mp4s detection. closes #26
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-vasile committed Mar 16, 2019
1 parent 1181625 commit 4ac81fa
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 18 deletions.
27 changes: 10 additions & 17 deletions matchers/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,23 @@ package matchers

import (
"bytes"
"encoding/binary"
)

// Mp4 matches an MP4 file.
// See http://www.ftyps.com for more ftyps and mimetypes
func Mp4(in []byte) bool {
if len(in) < 12 {
if !bytes.Equal(in[4:8], []byte("ftyp")) {
return false
}

mp4ftype := []byte("ftyp")
mp4 := []byte("mp4")
boxSize := int(binary.BigEndian.Uint32(in[:4]))
if boxSize%4 != 0 || len(in) < boxSize {
return false
}
if !bytes.Equal(in[4:8], mp4ftype) {
return false
ftyps := []string{
"avc1", "dash", "iso2", "iso3", "iso4", "iso5", "iso6", "isom", "mmp4",
"mp41", "mp42", "mp4v", "mp71", "MSNV", "NDAS", "NDSC", "NSDC", "NSDH",
"NDSM", "NDSP", "NDSS", "NDXC", "NDXH", "NDXM", "NDXP", "NDXS", "F4V ",
"F4P ",
}
for st := 8; st < boxSize; st += 4 {
if st == 12 {
// minor version number
continue
}
if bytes.Equal(in[st:st+3], mp4) {

for _, ftyp := range ftyps {
if bytes.Equal(in[8:12], []byte(ftyp)) {
return true
}
}
Expand Down
3 changes: 2 additions & 1 deletion mime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var files = map[string]*Node{

// video
"a.mp4": Mp4,
"b.mp4": Mp4,
"a.webm": WebM,
"a.3gp": ThreeGP,
"a.flv": Flv,
Expand All @@ -60,7 +61,7 @@ var files = map[string]*Node{
"a.html": Html,
"a.xml": Xml,
"a.svg": Svg,
"a1.svg": Svg,
"b.svg": Svg,
"a.txt": Txt,
"a.php": Php,
"a.ps": Ps,
Expand Down
Binary file added testdata/b.mp4
Binary file not shown.
File renamed without changes

0 comments on commit 4ac81fa

Please sign in to comment.