如何将大视频分割为块,每个大小为5 MB,Swift

b4qexyjb  于 2023-01-19  发布在  Swift
关注(0)|答案(1)|浏览(127)

我的工作视频上传过程中,需要上传大视频作为多个块每个块应该是5 MB的大小
请分享代码转换大视频到多个块下的大小为5 MB的每个在swift。任何帮助是非常感谢

mrphzbgm

mrphzbgm1#

试试这个

let selectedSize = selectedVideoSizeInMB
    let seconds = durationInSeconds
    print("Size", selectedSize)
    print("Seconds", seconds)
    
    let splitDuration: Int = Int(seconds / (selectedSize/5))
    print("Split Duration", splitDuration)
    
    do {
        let data = try Data(contentsOf: videoUrl)
        let dataLen = data.count
        print("Data Size", data.count)
        let chunkSize = ((1024 * 1000) * 5) // MB
        print("Chunk Size", chunkSize)
        let fullChunks = Int(dataLen / chunkSize)
        let totalChunks = fullChunks + (dataLen % 1024 != 0 ? 1 : 0)
        print("Chunk count", totalChunks)
        
        var chunks:[Data] = [Data]()
        for chunkCounter in 0..<totalChunks {
            var chunk:Data
            let chunkBase = chunkCounter * chunkSize
            var diff = chunkSize
            if(chunkCounter == totalChunks - 1) {
                diff = dataLen - chunkBase
            }
            
            let range:Range<Data.Index> = chunkBase..<(chunkBase + diff)
            chunk = data.subdata(in: range)
            chunks.append(chunk)
            print("The size is \(chunk.count)")
        }
        self.chunksDataArray = chunks
        print("Total Chunk", chunks.count)
    }catch{
        return
    }

相关问题