11#! /bin/sh
22
3- # Pre-requisites (need to be installed on this system to build FFmpeg) :
3+ # common.sh
4+ # Aural-macOS
45#
5- # 1 - Homebrew (Download instructions here: https://brew.sh/)
6- # 2 - nasm - assembler for x86 (Run "brew install nasm" ... after installing Homebrew)
7- # 3 - clang - C compiler (Run "xcode-select --install")
6+ # Created by Kartik Venugopal on 22/09/22.
7+ # Copyright © 2024 Kartik Venugopal. All rights reserved.
88
9- # MARK: Constants -------------------------------------------------------------------------------------
9+ # Common variables and functions used across the ffmpeg build scripts.
1010
11- # FFmpeg release version
12- export ffmpegVersion=" 4.4"
11+ # MARK: Constants -------------------------------------------------------------------------------------
1312
1413# Architectures
1514export architectures=(" x86_64" " arm64" )
1615
17- # Library versions
18- export avcodecVersion=58
19- export avformatVersion=58
20- export avutilVersion=56
21- export swresampleVersion=3
16+ # Deployment target for Aural Player.
17+ export deploymentTarget=" 11.0"
18+
19+ # Points to the latest MacOS SDK installed.
20+ export sdk=" /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk"
21+
22+ # FFmpeg release version
23+ export ffmpegVersion=" 7.0.1"
2224
2325export avcodecLibName=" libavcodec"
2426export avformatLibName=" libavformat"
2527export avutilLibName=" libavutil"
2628export swresampleLibName=" libswresample"
2729
28- export libNames=($avcodecLibName $avformatLibName $avutilLibName $swresampleLibName )
29-
30- export avcodecHeaderNames=(" ac3_parser.h" " adts_parser.h" " avcodec.h" " avdct.h" " avfft.h" " bsf.h" " codec.h" " codec_desc.h" " codec_id.h" " codec_par.h" " d3d11va.h" " dirac.h" " dv_profile.h" " dxva2.h" " jni.h" " mediacodec.h" " packet.h" " qsv.h" " vaapi.h" " vdpau.h" " version.h" " videotoolbox.h" " vorbis_parser.h" " xvmc.h" )
31-
32- export avformatHeaderNames=(" avformat.h" " avio.h" " version.h" )
33-
34- export avutilHeaderNames=("adler32.h" "aes.h" "aes_ctr.h" "attributes.h" "audio_fifo.h" "avassert.h" "avconfig.h" "avstring.h" "avutil.h" "base64.h" "blowfish.h" "bprint.h" "bswap.h" "buffer.h" "camellia.h" "cast5.h" "channel_layout.h" "common.h" "cpu.h" "crc.h" "des.h" "dict.h" "display.h" "dovi_meta.h" "downmix_info.h" "encryption_info.h" "error.h" "eval.h" "fifo.h" "file.h" "film_grain_params.h" "frame.h" "hash.h" "hdr_dynamic_metadata.h" "hmac.h" "hwcontext.h" "hwcontext_cuda.h" "hwcontext_d3d11va.h" "hwcontext_drm.h" "hwcontext_dxva2.h" "hwcontext_mediacodec.h" "hwcontext_opencl.h" "hwcontext_qsv.h" "hwcontext_vaapi.h" "hwcontext_vdpau.h" "hwcontext_videotoolbox.h" "hwcontext_vulkan.h" "imgutils.h" "intfloat.h" "intreadwrite.h" "lfg.h" "log.h" "lzo.h" "macros.h" "mastering_display_metadata.h" "mathematics.h" "md5.h" "mem.h" "motion_vector.h" "murmur3.h" "opt.h" "parseutils.h" "pixdesc.h" "pixelutils.h" "pixfmt.h" "random_seed.h" "rational.h" "rc4.h" "replaygain.h" "ripemd.h" "samplefmt.h" "sha.h" "sha512.h" "spherical.h" "stereo3d.h" "tea.h" "threadmessage.h" "time.h" "timecode.h" "timestamp.h" "tree.h" "twofish.h" "tx.h" "version.h" "video_enc_params.h" "xtea.h")
35-
36- export swresampleHeaderNames=(" swresample.h" " version.h" )
30+ # Library versions
31+ export avcodecVersion=61
32+ export avformatVersion=61
33+ export avutilVersion=59
34+ export swresampleVersion=5
3735
3836# Aliases for the library files.
3937export avcodecLib=" ${avcodecLibName} .${avcodecVersion} .dylib"
4038export avformatLib=" ${avformatLibName} .${avformatVersion} .dylib"
4139export avutilLib=" ${avutilLibName} .${avutilVersion} .dylib"
4240export swresampleLib=" ${swresampleLibName} .${swresampleVersion} .dylib"
4341
42+ # Names of public headers to include in XCFrameworks
43+ export avcodecHeaderNames=(" ac3_parser.h" " adts_parser.h" " avcodec.h" " avdct.h" " avfft.h" " bsf.h" " codec.h" " codec_desc.h" " codec_id.h" " codec_par.h" " d3d11va.h" " defs.h" " dirac.h" " dv_profile.h" " dxva2.h" " jni.h" " mediacodec.h" " packet.h" " qsv.h" " vdpau.h" " version.h" " version_major.h" " videotoolbox.h" " vorbis_parser.h" )
44+
45+ export avformatHeaderNames=(" avformat.h" " avio.h" " version.h" " version_major.h" )
46+
47+ export avutilHeaderNames=("adler32.h" "aes.h" "aes_ctr.h" "attributes.h" "audio_fifo.h" "avassert.h" "avconfig.h" "avstring.h" "avutil.h" "base64.h" "blowfish.h" "bprint.h" "bswap.h" "buffer.h" "camellia.h" "cast5.h" "channel_layout.h" "common.h" "cpu.h" "crc.h" "des.h" "dict.h" "display.h" "dovi_meta.h" "downmix_info.h" "encryption_info.h" "error.h" "eval.h" "fifo.h" "file.h" "film_grain_params.h" "frame.h" "hash.h" "hdr_dynamic_metadata.h" "hmac.h" "hwcontext.h" "hwcontext_cuda.h" "hwcontext_d3d11va.h" "hwcontext_drm.h" "hwcontext_dxva2.h" "hwcontext_mediacodec.h" "hwcontext_opencl.h" "hwcontext_qsv.h" "hwcontext_vaapi.h" "hwcontext_vdpau.h" "hwcontext_videotoolbox.h" "hwcontext_vulkan.h" "imgutils.h" "intfloat.h" "intreadwrite.h" "lfg.h" "log.h" "lzo.h" "macros.h" "mastering_display_metadata.h" "mathematics.h" "md5.h" "mem.h" "motion_vector.h" "murmur3.h" "opt.h" "parseutils.h" "pixdesc.h" "pixelutils.h" "pixfmt.h" "random_seed.h" "rational.h" "rc4.h" "replaygain.h" "ripemd.h" "samplefmt.h" "sha.h" "sha512.h" "spherical.h" "stereo3d.h" "tea.h" "threadmessage.h" "time.h" "timecode.h" "timestamp.h" "tree.h" "twofish.h" "tx.h" "version.h" "version_major.h" "video_enc_params.h" "xtea.h")
48+
49+ export swresampleHeaderNames=(" swresample.h" " version.h" " version_major.h" )
50+
4451# The name of the FFmpeg source archive file.
45- export srcArchiveName=" ffmpeg-${ffmpegVersion} .tar.bz2 "
52+ export srcArchiveName=" ffmpeg-${ffmpegVersion} .tar.xz "
4653
4754# The name of the FFmpeg source directory (once the archive has been uncompressed).
4855export srcDirName=" ffmpeg-${ffmpegVersion} "
4956
50- # Deployment target for Aural Player.
51- export minMacOSVersion=" 10.12"
57+ # MARK: Functions -------------------------------------------------------------------------------------
5258
53- # Points to the latest MacOS SDK installed.
54- export sdk=" /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk"
59+ function runBuild {
5560
56- # MARK: Functions -------------------------------------------------------------------------------------
61+ cleanXCFrameworksDir
62+ buildFFmpeg
63+ createFatLibs
64+ copyHeaders
65+ }
5766
5867function cleanXCFrameworksDir {
5968
@@ -62,8 +71,19 @@ function cleanXCFrameworksDir {
6271 fi
6372}
6473
74+ function extractSources {
75+
76+ # Extract source code from archive.
77+ if [ ! -d ${srcDirName} ]; then
78+ tar xjf ${srcArchiveName}
79+ fi
80+ }
81+
6582function buildFFmpeg {
6683
84+ extractSources
85+
86+ # Run all builds in parallel and wait till they all finish.
6787 for arch in ${architectures[@]} ; do
6888 buildFFmpegForArch $arch &
6989 done
@@ -74,41 +94,61 @@ function buildFFmpeg {
7494function buildFFmpegForArch {
7595
7696 arch=$1
97+
7798 echo " \nBuilding FFmpeg for architecture '${arch} ' ..."
7899
79- # Extract source code from archive
80- srcBaseDir=" src- ${arch} "
81- mkdir ${srcBaseDir}
82- tar xjf ${srcArchiveName} -C ${ srcBaseDir}
100+ # Make a copy of the source code.
101+ srcBaseDir=" src/ ${arch} "
102+ mkdir -p ${srcBaseDir}
103+ cp -r $srcDirName / * $ srcBaseDir
83104
84- configureAndMake $arch
105+ configureFFmpeg $arch $srcBaseDir
106+ makeFFmpeg
107+
85108 copyLibs $arch
86109 fixInstallNames $arch
87110}
88111
89- function configureAndMake {
112+ # Determine compiler / linker flags based on architecture.
113+ function setCompilerAndLinkerFlags {
90114
91115 arch=$1
92116
93- # CD to the source directory.
94- cd " src-${arch} /${srcDirName} "
117+ export compiler=" /usr/bin/clang"
118+
119+ # Architecture of the host machine running this build.
120+ hostArchitecture=$( uname -m)
95121
96- # Determine compiler / linker flags based on architecture.
97- if [[ " $arch " == " arm64" ]]
122+ if [[ " $arch " != " $hostArchitecture " ]]
98123 then
99- archInFlags=" -arch arm64 "
100- crossCompileAndArch=" --enable-cross-compile --arch=arm64"
101- else
102- archInFlags=" "
103- crossCompileAndArch=" "
124+ archInFlags=" -arch ${arch} "
125+ export crossCompileOption=" --enable-cross-compile"
126+ export archOption=" --arch=${arch} "
104127 fi
105128
129+ export extraCompilerFlags=" ${archInFlags} -mmacosx-version-min=${deploymentTarget} -isysroot ${sdk} "
130+ export extraLinkerFlags=${extraCompilerFlags}
131+ }
132+
133+ function configureFFmpeg {
134+
135+ arch=$1
136+ srcDir=$2
137+
138+ # CD to the source directory.
139+ cd ${srcDir}
140+
141+ setCompilerAndLinkerFlags $arch
142+
106143 # Configure FFmpeg
107144 ./configure \
108- --cc=/usr/bin/clang \
109- --extra-ldflags=" ${archInFlags} -mmacosx-version-min=${minMacOSVersion} -isysroot ${sdk} " \
110- --extra-cflags=" ${archInFlags} -mmacosx-version-min=${minMacOSVersion} -isysroot ${sdk} " \
111- ${crossCompileAndArch} \
145+ --target-os=darwin \
146+ ${archOption} \
147+ --cc=" ${compiler} " \
148+ --as=" ${assembler} " \
149+ --extra-cflags=" ${extraCompilerFlags} " \
150+ --extra-ldflags=" ${extraLinkerFlags} " \
151+ ${crossCompileOption} \
112152 --enable-gpl \
113153 --enable-version3 \
114154 --enable-shared \
@@ -118,14 +158,15 @@ function configureAndMake {
118158 --enable-pthreads \
119159 --disable-doc \
120160 --disable-debug \
161+ --disable-network \
121162 --disable-all \
122163 --enable-avcodec \
123164 --enable-avformat \
124165 --enable-swresample \
125- --enable -avfoundation \
126- --enable -audiotoolbox \
127- --enable -coreimage \
128- --enable -zlib \
166+ --disable -avfoundation \
167+ --disable -audiotoolbox \
168+ --disable -coreimage \
169+ --disable -zlib \
129170 --disable-everything \
130171 --disable-appkit \
131172 --disable-iconv \
@@ -137,7 +178,10 @@ function configureAndMake {
137178 --enable-parsers \
138179 --enable-decoders \
139180 --enable-protocol=file
140-
181+ }
182+
183+ function makeFFmpeg {
184+
141185 # Build FFmpeg (use multithreading).
142186 tokens=$( sysctl hw.physicalcpu)
143187 numCores=" $( cut -d' ' -f2 <<< $tokens ) "
@@ -151,10 +195,11 @@ function copyLibs {
151195 arch=$1
152196
153197 # Create the directory where the libs will be installed in.
154- mkdir -p " dylibs/${arch} "
155- cd " dylibs/${arch} "
198+ libsDir=" dylibs/${arch} "
199+ mkdir -p $libsDir
200+ cd $libsDir
156201
157- srcDir=" ../../src- ${arch} /${srcDirName } "
202+ srcDir=" ../../src/${arch } "
158203
159204 cp ${srcDir} /${avcodecLibName} /${avcodecLib} .
160205 cp ${srcDir} /${avformatLibName} /${avformatLib} .
@@ -184,45 +229,63 @@ function fixInstallNames {
184229 install_name_tool -id @rpath/${swresampleLib} ${swresampleLib}
185230 install_name_tool -change /usr/local/lib/${avutilLib} @rpath/${avutilLib} ${swresampleLib}
186231
187- cd ../..
232+ cd ../../..
188233}
189234
190235function createFatLibs {
191236
192237 # Combine x86_64 and arm64 dylibs into "fat" universal dylibs.
193-
194- cd dylibs/x86_64
195- for file in * ; do
196- lipo " ${file} " " ../arm64/${file} " -output " ../${file} " -create
238+ cd dylibs
239+
240+ allDylibs=($avcodecLib $avformatLib $avutilLib $swresampleLib )
241+
242+ for lib in ${allDylibs[@]} ; do
243+
244+ nonFatLibFiles=" "
245+
246+ for arch in ${architectures[@]} ; do
247+ nonFatLibFiles=" ${nonFatLibFiles} ${arch} /${lib} "
248+ done
249+
250+ lipo ${nonFatLibFiles} -output ${lib} -create
251+
197252 done
198253
199- cd ../..
254+ cd ..
200255}
201256
202257function copyHeaders {
203258
204259 mkdir " headers"
205260
206- copyHeadersForLib $avcodecLibName " ${avcodecHeaderNames[@]} "
207- copyHeadersForLib $avformatLibName " ${avformatHeaderNames[@]} "
208- copyHeadersForLib $avutilLibName " ${avutilHeaderNames[@]} "
209- copyHeadersForLib $swresampleLibName " ${swresampleHeaderNames[@]} "
261+ srcBaseDir=" src/arm64"
262+ headersBaseDir=" headers"
263+
264+ copyHeadersForLib $srcBaseDir $avcodecLibName $headersBaseDir " ${avcodecHeaderNames[@]} "
265+ copyHeadersForLib $srcBaseDir $avformatLibName $headersBaseDir " ${avformatHeaderNames[@]} "
266+ copyHeadersForLib $srcBaseDir $avutilLibName $headersBaseDir " ${avutilHeaderNames[@]} "
267+ copyHeadersForLib $srcBaseDir $swresampleLibName $headersBaseDir " ${swresampleHeaderNames[@]} "
210268}
211269
212270function copyHeadersForLib {
213271
214- libName=$1
272+ srcBaseDir=$1
273+ libName=$2
274+ headersBaseDir=$3
275+ shift
276+ shift
215277 shift
216278 headerNames=(" $@ " )
217- srcDir=" src-arm64/ ${srcDirName } /${libName} "
279+ srcDir=" ${srcBaseDir } /${libName} "
218280
219281 # Add a 2nd level folder with the same library name (otherwise headers are not resolved properly in the XCode project).
220282 # For example: "/libavcodec/libavcodec/someHeader.h"
221283
222- mkdir -p " headers/${libName} /${libName} "
284+ headersDestDir=" ${headersBaseDir} /${libName} /${libName} "
285+ mkdir -p $headersDestDir
223286
224287 for file in ${headerNames[@]} ; do
225- cp " ${srcDir} /${file} " " ./headers/ ${libName} / ${libName }"
288+ cp " ${srcDir} /${file} " " ${headersDestDir }"
226289 done
227290}
228291
@@ -246,33 +309,16 @@ function createXCFrameworkForLib {
246309 -output " xcframeworks/${libName} .xcframework"
247310}
248311
249- function deleteSource {
250-
251- for arch in ${architectures[@]} ; do
252- rm -rf " src-${arch} "
253- done
254- }
312+ function cleanUp {
255313
256- function deleteDylibs {
314+ rm -rf ${srcDirName}
315+ rm -rf src
257316 rm -rf dylibs
258- }
259-
260- function deleteHeaders {
261317 rm -rf headers
262318}
263319
264- # MARK: Script -------------------------------------------------------------------------------------
265-
266- cleanXCFrameworksDir
267- buildFFmpeg
268-
269- createFatLibs
270- copyHeaders
271-
320+ runBuild
272321createXCFrameworks
273-
274- deleteSource
275- deleteDylibs
276- deleteHeaders
322+ cleanUp
277323
278324echo " \nAll done !\n"
0 commit comments