简体   繁体   中英

How to differentiate a MP3,MP4 file?

I don't want to use pathExtension to do this, as it's not rigorous. I differentiated image files by the file header, but I can't find any information about this for MP3/MP4.

(Either Swift or OC is OK)

To determine it's a mp3, look at the first two or three bytes of the file. If it's 49 44 33 or ff fb , you have a mp3 .

And a signature of ftyp may indicate a.mp4 file .

Here's some Swift code I worked up for this purpose:

import Foundation

var c = 0;
for arg in Process.arguments {
    print("argument \(c) is: \(arg)")
    c++
}

let pathToFile = Process.arguments[1]

do {
    let fileData = try NSData.init(contentsOfFile: pathToFile, options: NSDataReadingOptions.DataReadingMappedIfSafe)

    if fileData.length > 0
    {
        let count = 8

        // create array of appropriate length:
        var array = [UInt8](count: count, repeatedValue: 0)

        // copy bytes into array
        fileData.getBytes(&array, length:count * sizeof(UInt8))

        var st = String(NSString(format:"%02X %02X %02X %02X %02X %02X %02X %02X",
            array[0], array[1], array[2], array[3], array[4], array[5], array[6], array[7]))

        // 49 44 33 is mp3

        //
        print("first \(count) bytes are \(st)")

        // f t y p seems to determine a .mp4 file
        st = String(NSString(format:"%c %c %c %c %c %c %c %c",
            array[0], array[1], array[2], array[3], array[4], array[5], array[6], array[7]))

        print("first \(count) bytes are \(st)")
    }
} catch let error as NSError {
    print("error while trying to read from \(pathToFile) - \(error.localizedDescription)")
}

You'd want to do what you did with images and use the header information in both files to determine whether it's an mp3 file, or an mp4 file. There's information about the mp4 header here: Anyone familiar with mp4 data structure? .

Swift 5

func doStuff(fileData: Data) {
    if fileData.count > 0 {
        let count = 8

        // create array of appropriate length:
        var array = [UInt8](unsafeUninitializedCapacity: count) { buffer, initializedCount in
            for x in 0..<count { buffer[x] = 0 }
            initializedCount = count
        }

        // copy bytes into array
        fileData.copyBytes(to: &array, count: count * UInt8.bitWidth)

        var st = String(NSString(format: "%02X %02X %02X %02X %02X %02X %02X %02X",
            array[0], array[1], array[2], array[3], array[4], array[5], array[6], array[7]))

        // 49 44 33 is mp3
        print("first \(count) bytes are \(st)")

        // f t y p seems to determine a .mp4 file
        st = String(NSString(format: "%c %c %c %c %c %c %c %c",
            array[0], array[1], array[2], array[3], array[4], array[5], array[6], array[7]))

        print("first \(count) bytes are \(st)")
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM