Search code examples
iosswiftxcodewatchkitbit

How can I change the first two bits of an mp4 file to 00, 00 in swift?


I am trying to change the first two bits of an mp4 file to 00, 00 to change it back to normal / working state. After downloading the mp4 file from an API, I discovered it would not play and acted weird, so on the internet I found out that some people said it was encoded and changing the two first bits to 0 makes it work! (It does, but) I don't know how to do that in swift any help would be appreciated!


Solution

  • I found out how to do it! This is the code:

    import UIKit
    import AVFoundation
    
    
    
    // URL to a mp4 file called videoFile located in the project's folder:
    let videoURL = URL(string: Bundle.main.path(forResource: "videoFile", ofType: "mp4")!)
    
    
    
    // Func for "decoding" (changing it's 2 first bytes to 0):
    func changeFirstTwoBytesOfFile(fileURL: URL) {
    
        var fileData = fileURL.dataRepresentation
    
        // Printing first 4 bytes of the unchanged file for checking:
        print("Bytes before change:")
    
        print(fileData[0])
        print(fileData[1])
        print(fileData[2])
        print(fileData[3])
    
        print("")
        print("")
    
    
    
        // Changing it's 2 first bytes to 0
        fileData[0] = 0
        fileData[1] = 0
    
    
    
        // Printing the changed file's bytes to check if the change took place:
        print("Bytes after change:")
    
        print(fileData[0])
        print(fileData[1])
        print(fileData[2])
        print(fileData[3])
    }
    
    
    changeFirstTwoBytesOfFile(fileURL: videoURL!)