0

我正在使用mobileffmpeg cocoapod使用以下命令将图像连接到视频中,但是视频质量在完成连接后会有所下降。

-f concat -i \(outputTxt) -b:v 8M -pattern_type sequence -r 25 \(output)

我可以看到图像阵列不是问题,因为质量很好,所以在连接时如何避免质量下降?我想也许使用libx264可以解决这个问题,但mobileffmpeg不支持它

下面是我在 Swift 中用来连接的函数,如果它有助于理解这个过程的话。谢谢你的帮助!

func concatonateScreenshotsToVideo( completed: @escaping (URL) -> ()){
    if compilingVideo {
        self.delegate?.screenRecordingConcatonating()

            var imgs = self.storedTempScreenshotImgArray()
            imgs.sort()
            
            self.createFFmpegTextFile(tempScreenshotImgArray: imgs) {
                let filename = "screenshot_ffmpegData.txt"
                let outputTxt = URL(fileURLWithPath: self.mainPath + "/Temp/").appendingPathComponent(filename)
                let output = URL(fileURLWithPath: self.mainPath + "/Temp/").appendingPathComponent("screenshot.mp4")
                
                let ffmpeg =  "-f concat -i \(outputTxt) -b:v 8M -pattern_type sequence -r \(self.fps) \(output)"
                
                MobileFFmpeg.execute(ffmpeg)
                completed(output)
            }
    } else {
        terminateFFmpeg()
        return
    }
}

func createFFmpegTextFile(tempScreenshotImgArray: [String], completed: () -> Void){
    let filename = "screenshot_ffmpegData.txt"
    let textFile = URL(fileURLWithPath: mainPath).appendingPathComponent("Temp/\(filename)")
    
    for img in tempScreenshotImgArray {
        autoreleasepool {
            do {
                let fileHandle = try FileHandle(forWritingTo: textFile)
                fileHandle.seekToEndOfFile()
                
                let filepath = "file \(img)\n duration 0.04\n"
                fileHandle.write(filepath.data(using: .utf8)!)
                
            } catch {
                do {
                    let filePath = "file \(img)\n duration 0.04\n"
                    try filePath.write(to: textFile, atomically: false, encoding: .utf8)
                } catch {
                    DebugPrint.DBprint("FFmpeg manager error writing text file: \(error.localizedDescription)")
                }
            }
        }
    }

func storedTempScreenshotImgArray() -> [String] {
    var screenshotImgArray: [String] = []
    
    guard let dataFilePath = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent("/Temp") else {return screenshotImgArray}
    
    do {
        let fileUrls = try fileManager.contentsOfDirectory(at: dataFilePath, includingPropertiesForKeys: nil)
        for jpgFile in fileUrls {
            if jpgFile.pathExtension == "jpg" && !screenshotImgArray.contains(jpgFile.lastPathComponent) {
                screenshotImgArray.append(jpgFile.lastPathComponent)
            }
        }
    } catch {
        DebugPrint.DBprint("screenshot - Error while enumerating folder segment")
    }
    
    return screenshotImgArray
    
}
4

0 回答 0