03/11/2011

Determin mp4 file is audio file or video file

If you receive mp4 file from server, you can determin it is audio file or video file by mime information which is sent by server. However, if mp4 file is in your Android terminal (internal or external storage device), how do you solve the problem? In this small post, I present a solution use MP4Parser library.

try{
//mp4 is path string of your mp4 file   

IsoBufferWrapper isoBufferWrapper = new IsoBufferWrapper(new File(mp4));
IsoFile isoFile = new IsoFile(isoBufferWrapper);
isoFile.parse();
//Get FileTypeBox   
    ArrayList boxes =( ArrayList) isoFile.getBoxes(FileTypeBox.class);
    FileTypeBox ftb =(FileTypeBox) boxes.get(0);
    String major =ftb.getMajorBrand().trim().toLowerCase();
    //The list of ftyps http://www.ftyps.com/
    if(major.equals("m4a") || //Apple iTunes AAC-LC (.M4A) Audio
       major.equals("msnv")|| //MPEG-4 (.MP4) for SonyPSP
       major.equals("ndas")|| //MP4 v2 [ISO 14496-14] Nero Digital AAC Audio
       major.equals("da1a")|| //DMB MAF audio with ER-BSAC audio, JPG/PNG/MNG images
       major.equals("f4a")|| //Audio for Adobe Flash Player 9+ (.F4A)
       major.equals("f4b")|| //Audio Book for Adobe Flash Player 9+ (.F4B)
       major.equals("m4b")|| //Apple iTunes AAC-LC (.M4B) Audio Book
       major.equals("m4p")|| //Apple iTunes AAC-LC (.M4P) AES Protected Audio
       major.equals("bsac")|| //MPEG-4 "Bit Sliced Arithmetic Coding" (audio)
       major.equals("mot slides") //Digital Audio Broadcasting (DAB) MOT slideshow (ETSI DAB  TR 101 497)
            ){
        //AUDIO FILE
    }else{
        //VIDEO FILE
    }
        }catch(IOException ex){
            ex.printStackTrace();
            //ERROR
        }

Bài viết liên quan



Không có nhận xét nào:

Đăng nhận xét