iOS音視頻—FFmepg:iOS平臺下集成和應用

1.在iOS平臺下集成和應用FFmpeg

Mac配置FFmpeg環境

1、安裝homebrew

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

2、安裝FFmpeg

1)利用上面的homebrew安裝FFmpeg:

brew install ffmpeg

2)當命令結束之後,輸入以下命令(查看你的安裝ffmpeg的信息):

brew info ffmpeg

3、 iOS集成FFmpeg

下載腳本FFmpeg腳本地址

編譯FFmpeg-iOS-build-script,獲得FFmpeg靜態庫文件

打開腳本文件,然後使用終端運行:

cd 到FFmpeg-iOS-build-script-master目錄下
./build-ffmpeg.sh

編譯的時間略長,請耐心等待。可能出現下面的問題

xcrun -sdk iphoneos clang is unable to create an executable file.
C compiler test failed.
 
If you think configure made a mistake, make sure you are using the latest
version from Git. If the latest version fails, report the problem to the
[email protected] mailing list or IRC #ffmpeg on irc.freenode.net.
Include the log file "config.log" produced by configure as this will help
solve the problem.

解決方法:打開自己電腦的應用程序,查看自己正在使用的Xcode是否在/Applications/文件目錄下,運行下面的命令

sudo xcode-select --switch /Applications/Xcode.app

注意:上面命令中Xcode.app是在/Applications/目錄下的命名,如果名稱不是Xcode.app則相對更改。

在iOS平臺下集成和應用FFmpeg

1、新建項目
2、上步操作執行成功後,會生成FFmpeg-iOS文件,先在項目中創建FFmpeg文件,將FFmpeg-iOS文件中的includelib文件複製到FFmpeg文件中。
3、配置頭文件搜索路徑:在工程文件->Bulid Setting->Search Paths->Header Search Paths添加$(SRCROOT)/$(PRODUCT_NAME)/FFmpeg/include(如下圖),然後搜索library Search paths 添加$(SRCROOT)/$(PRODUCT_NAME)/FFmpeg/lib

在這裏插入圖片描述

4、添加需要的靜態庫:CoreMedia.framework,VideoToolbox.framework,CoreGraphics.framework,AudioToolbox.framework,libiconv.tbd,libz.tbd,libbz2.tbd
5、在代碼中測試FFmpeg測試配置信息
5.1 引入頭文件->FFmpeg
//測試FFmpeg配置

FFmpegTest.h

#import <Foundation/Foundation.h>

//引入頭文件
//核心庫->音視頻編解碼庫
#import <libavcodec/avcodec.h>

@interface FFmpegTest : NSObject

//測試FFmpeg配置
+(void)ffmpegTestConfig;

@end
5.2 定義測試FFmpeg方法

FFmpegTest.m

#import "FFmpegTest.h"

@implementation FFmpegTest

//測試FFmpeg配置
+(void)ffmpegTestConfig{
    const char * configuration = avcodec_configuration();
    NSLog(@"配置信息:%s",configuration);
}
@end

ViewController.m

#import "ViewController.h"
#import "FFmpegTest.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [FFmpegTest ffmpegTestConfig];
}

案例:指定打開一個視頻文件

FFmpegTest.h

//導入封裝格式庫
#import <libavformat/avformat.h>

//打開視頻文件
+(void)ffmpegOpenfile:(NSString *)filePath;

FFmpegTest.m

//打開視頻文件
+(void)ffmpegOpenfile:(NSString *)filePath{
    //1.註冊組件
    av_register_all();
    
    //2.打開封裝格式文件
    /*
     @param 封裝格式上下文
     @param 打開視頻地址->path
     @param 指定輸入封裝格式
     @param 指定默認配置信息
     @return 0->成功 失敗返回averror
     */
    AVFormatContext * avformat_context = avformat_alloc_context();
    const char *url = [filePath UTF8String];
    int avformat_open_input_result = avformat_open_input(&avformat_context,url, NULL, NULL);
    if (avformat_open_input_result !=0) {
        NSLog(@"打開文件失敗");
        return;
    }
    NSLog(@"打開文件成功");
}

參考Demo


?的內容可以忽略

自己編寫的shell腳本(存在問題,以後再看看)

ffmpeg-bulid.sh文件

#!/bin/bash

#1、首先定義下載的庫名稱
source="ffmpeg-3.4"

#2、其次定義".h/.m/.c"文件編譯的結果目錄
# 目錄作用:用於保存.h/.m/.c文件編譯後的結果.o文件
cache="cache"

#3、定義”.a“靜態庫保存目錄
# pwd命令:表示獲取當前目錄
staticdir = 'pwd'/"WT-ffmpeg-iOS"

#4、添加FFmpeg配置選項->默認配置
#Toolchain options:工具鏈選項(指定我們需要編譯平臺CPU架構類型,例如:arm64\x86等等...)
#--enable-cross-compile:交叉編譯
#Developer options:開發者選項
#--disable-debug:禁止使用調試模式
#Program options 選項
#--disable-programs:禁用程序(不允許建立命令行程序)
#Documentation options:文檔選項
#--disable-doc:不需要編譯文檔
#Toolchain options:工具鏈選項
#--enable-pic:允許建立與位置無關代碼
configure_flags='--enable-cross-compile --disable-debug --disable-programs --disable-doc --enable-pic'
#核心庫-音視頻編解碼庫(最重要的庫):avcodec
configure_flags='$configure_flags --disable-avdevice --enable-avcodec --disable-avformat'
configure_flags="$configure_flags --disable-swresample --disable-swscale --disable-postproc"
configure_flags="$configure_flags --disable-avfilter --enable-avutil --enable-avresample"

#5、定義默認CPU平臺架構類型
#arm64 armv7 ->真機 ->CPU架構類型
#x86_64 i386 ->模擬器 ->CPU架構類型
archs="arm64 armv7 x86_64 i386"

#6、指定我們的這個庫編譯系統版本->iOS系統下的7.0及以上版本使用這個靜態庫
targetversion="7.0"

#7、接受命令後輸入參數
#我是動態接受命令行輸入CPU平臺架構類型(輸入參數:編譯指定的CPU庫)
if [ "$*" ]
then
    #存在輸入參數,也就是說:外部指定需要編譯CPU架構類型
    archs="$*"
fi

#8、安裝彙編器->yasm
#判斷一下是否存在這個彙編器
#目的:通過軟件管理器(Homebrew),然後下載安裝(或者更新)彙編器
#一個命令就能解決我們完成所有的操作

if [ ! 'which yasm' ]
then
    #Homebrew:軟件管理器
    #下載一個軟件管理器:安裝,卸載,更新,搜索等...
    if [ ! 'which brew' ]
    then
         echo "安裝brew"
         ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" || exit 1
    fi
    echo "安裝yasm"
    #成功了
    #下載安裝這個彙編器
    #exit 1->安裝失敗了,那麼退出程序
    brew install yasm || exit 1
fi

echo "循環編譯"

#9、for循環編譯FFmpeg靜態庫
currentdir='pwd'
for arch in $archs
do
  echo "開始編譯"
  #9.1 創建目錄
  #在編譯結果目錄下-創建對應的平臺架構類型
  mkdir -p "$cache/$arch"
  #9.2 進入目錄
  cd "$cache/$arch"

  #9.3 配置編譯CPU架構類型->指定當前編譯CPU架構類型
  archflags="-arch $arch"

  #9.4 判斷一下你到底是編譯的是模擬器.a靜態庫,還是真機.a靜態庫
  if [ "$arch" = "i386" -o "$arch" = "x86_64" ]
  then
      #模擬器
      platform="iPhoneSimulator"
      #支持最小系統版本->iOS系統
      archflags="$archflags -mios-simulator-version-min=$targetversion"
  else
      #真機(mac、iOS都支持)
      platform="iPhoneOS"
      #支持最小系統版本->iOS系統
      archflags="$archflags -mios-version-min=$targetversion -fembed-bitcode"
      #注意:優化處理(可有可無)
      #如果架構類型是“arm64”,那麼
      if [ "$arch" = "arm64" ]
      then
      #GNU彙編器(GNU Assembler),簡稱爲GAS
      #GASPP->彙編器預處理程序
      #解決問題:分段錯誤 通俗一點:就是程序運行時,變量訪問越界一類的問題
      EXPORT="GASPP_FIX_XCODE5=1"
      fi
   fi

#10、正式編譯
#tr命令可以對來自標準輸入的字符進行替換、壓縮和刪除
#'[:upper:]'->將小寫轉換成大寫
#'[:lower:]'->將大寫轉換成小寫
#將platform->轉成大寫或者小寫
XCRUN_SDK="echo $platform | tr '[:upper:]' '[:lower]'"
#編譯器->編譯平臺
CC="xcrun -sdk $XCRUN_SDK clang"

#架構類型->arm64
if [ "$arch" = "arm64" ]
then
    #音視頻默認一個編譯命令
    #preprocessor.pl幫助我們編譯FFmpeg->arm64位靜態庫
    AS="gas-preprocessor.pl -arch aarch64 -- $CC"
else
    #默認編譯平臺
    AS="$CC"
fi

echo "執行到了1"

#目錄找到FFmpeg編譯源代碼目錄->設置編譯配置->編譯FFmpeg源碼
#--target-os:目標系統->darwin(mac系統早期版本名字)
#darwin:是mac系統、iOS系統的祖宗
#--arch:CPU平臺架構類型
#--cc:指定編譯器類型選項
#--as:彙編程序
#$configure_flags最初配置
#--extra-cflags
#--prefix:靜態庫輸出目錄
TMPDIR=${TMPDIR/%\/} $currentdir/$source/configure\
--target-os=darwin\
--arch=$arch\
--cc="$CC"\
--as="$AS"\
$configure_flags\
--extra-cflags="$archflags"\
--extra-ldflags="$archflags"\
--prefix="$staticdir/$arch"\
|| exit 1

echo "執行了"
echo $TMPDIR

#解決問題->分段錯誤問題
#安裝->導出靜態庫(編譯.a靜態庫)
#執行命令
#將-j設置爲支持多核心/線程
make -j3 install $EXPORT || exit 1
#回到了我們的腳本文件目錄
cd $currentdir
done

FFmpeg在iOS上編譯

運行腳本

cd到腳本文件所在文件夾

cd /Users/mac/Desktop/GitHub/iOS_AudioVideoProcessing/ffmpeg-test
./ffmpeg-bulid.sh arm64
  • 注意一:一定要記得傳入參數arm64
  • 注意二:一定要將gas-preprocessor.pl導入到和腳本文件在同一個目錄下,
    先下載文件:https://github.com/libav/gas-preprocessor, 修改文件權限:
chmod 777 /usr/local/bin/gas-preprocessor.pl
  • 注意三:FFmpeg一共有9個庫(常用7個庫),可能3-4個庫需要,其他庫不需要。選擇性編譯一些庫,不一定編譯所有,如何選擇?
cd ffmpeg-3.4 目錄
./configure --help
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章