iOS 集成ffmpeg

前言

本来打算用flutter去实现一个ffmpeg的项目的,不过仔细分析了一下,我后期需要集成OpenGL ES做视频渲染处理,OpenGL ES的使用目前在flutter上面还不是很成熟,所以最后还是选择用原生来开发

ffmpeg集成到iOS工程

iOS对于ffmpeg的使用,看了网上,很多人说自己去编译ffmpeg源码,弄了半天,没弄好,放弃了,直接使用现成的第三方库 ffmpeg-kit-ios-full-gpl

这是我工程的Podfile文件

复制代码
platform:ios,'13.0'

target 'ffmpeg03' do
pod 'ffmpeg-kit-ios-full-gpl', '6.0'

end

遇到的问题

集成上面的裤,pod install之后,工程是运行不起来的,会报下面的错误:

Sandbox: rsync(31518) deny(1) file-write-create

解决方法

方法一:修改Targets -> Build Settings 中 ENABLE_USER_SCRIPT_SANDBOXING 设置 NO

方法二:项目使用cocoaPods进行三方管理 且 使用了 use_frameworks,把 use_frameworks 注释掉,重新pod install 然后运行

复制代码
platform:ios,'13.0'

target 'ffmpeg03' do
pod 'ffmpeg-kit-ios-full-gpl', '6.0'

end

编译通过

测试

工程目录:

项目代码

实现一个视频从mp4转avi

Swift 复制代码
import ffmpegkit
import UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        convertVideoFormat()
    }
    
    func convertVideoFormat() {
        // 获取输入文件路径
        guard let inputFile = Bundle.main.path(forResource: "12345", ofType: "mp4") else {
            print("未找到输入文件")
            return
        }
        
        // 设置输出文件路径
        let documentsDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let outputFile = "\(documentsDir)/output.avi"
        
        // 构建 FFmpeg 命令
        let ffmpegCommand = "-i \"\(inputFile)\" -c:v libx264 -c:a aac \"\(outputFile)\""
        print("执行的命令:ffmpeg \(ffmpegCommand)")
        
        // 执行转换
        FFmpegKit.executeAsync(ffmpegCommand) { session in
            let state = session?.getState()
            let returnCode = session?.getReturnCode()
            
            print("Return Code:\(String(describing: returnCode))")
            
            if ReturnCode.isSuccess(returnCode) {
                // 转换成功
                print("视频转换成功!输出文件位于:\(outputFile)")
                DispatchQueue.main.async {
                    // 在主线程中更新 UI 或进行下一步操作
                }
            } else {
                // 转换失败
                if let output = session?.getAllLogsAsString() {
                    print("转换失败,输出日志:\n\(output)")
                }
                if let failStackTrace = session?.getFailStackTrace() {
                    print("失败堆栈跟踪:\n\(failStackTrace)")
                }
            }
        }
    }
}

最终输出

相关推荐
简鹿办公8 小时前
FFmpeg vs 去水印软件:哪种方式更适合你?
ffmpeg·怎样去除视频水印·如何去视频logo视频水印
zhanggui10 小时前
iOS Debug Symbols
ios·xcode·debug symbox
库奇噜啦呼15 小时前
【iOS】static、const、extern关键字
ios
zzywxc78716 小时前
AI工具全景洞察:从智能编码到模型训练的全链路剖析
人工智能·spring·ios·prompt·ai编程
前端 贾公子17 小时前
《Vuejs设计与实现》第 16 章(解析器) 上
vue.js·flutter·ios
小狮子安度因19 小时前
ffplay数据结构分析
数据结构·ffmpeg
小狮子安度因1 天前
ffplay音频重采样
ffmpeg·音视频
Digitally1 天前
如何将大型音频文件从 iPhone 发送到不同的设备
ios·iphone
吴Wu涛涛涛涛涛Tao1 天前
Flutter 实现「可拖拽评论面板 + 回复输入框 + @高亮」的完整方案
android·flutter·ios