build.sh 5.05 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env bash

# This script builds FFmpeg libraries without any functional features.
#
# IMPORTANT:
# The resulting library files have to be LGPL version of FFmpeg libraries.
# - Do not enable `--enable-nonfree` and `--enable-gpl`.
# - Do not enable third party library integrations like x264.
#
# This script is not meant to build useful FFmpeg libraries, but to build
# a skeleton of FFmpeg libraries that are use only during the build process of
# torchaudio.
#
# The resulting FFmpeg libraries should not be shipped either.

set -eux

prefix="${FFMPEG_ROOT}"
args=""
if [[ "$OSTYPE" == "msys" ]]; then
   args="--toolchain=msvc"
fi
moto's avatar
moto committed
23
ffmpeg_version="${FFMPEG_VERSION:-4.1.8}"
24

moto's avatar
moto committed
25
archive="https://github.com/FFmpeg/FFmpeg/archive/refs/tags/n${ffmpeg_version}.tar.gz"
26

27
28
29
30
31
32
33
34
35
36
37
build_dir=$(mktemp -d -t ffmpeg-build.XXXXXXXXXX)
cleanup() {
    rm -rf "${build_dir}"
}
trap 'cleanup $?' EXIT

(
cd "${build_dir}"
# NOTE:
# When changing the version of FFmpeg, update the README so that the link to the source points
# the same version.
38
curl -LsS -o ffmpeg.tar.gz "${archive}"
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
tar -xf ffmpeg.tar.gz --strip-components 1
./configure \
    --prefix="${prefix}" \
    --disable-all \
    --disable-everything \
    --disable-programs \
    --disable-doc \
    --disable-debug \
    --disable-autodetect \
    --disable-x86asm \
    --disable-iconv \
    --disable-encoders \
    --disable-decoders \
    --disable-hwaccels \
    --disable-muxers \
    --disable-demuxers \
    --disable-parsers \
    --disable-bsfs \
    --disable-protocols \
    --disable-devices \
    --disable-filters \
    --disable-asm \
    --disable-static \
    --enable-shared \
    --enable-rpath \
    --enable-pic \
    --enable-avcodec \
    --enable-avdevice \
    --enable-avfilter \
    --enable-avformat \
    --enable-avutil ${args}

make -j install
ls ${prefix}/*
)

# macOS: Fix rpath so that the libraries are searched dynamically in user environment.
# In Linux, this is handled by `--enable-rpath` flag.
if [[ "$(uname)" == Darwin ]]; then
moto's avatar
moto committed
78
    major_ver=${ffmpeg_version:0:1}
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    if [[ ${major_ver} == 4 ]]; then
        avutil=libavutil.56
        avcodec=libavcodec.58
        avformat=libavformat.58
        avdevice=libavdevice.58
        avfilter=libavfilter.7
    elif [[ ${major_ver} == 5 ]]; then
        avutil=libavutil.57
        avcodec=libavcodec.59
        avformat=libavformat.59
        avdevice=libavdevice.59
        avfilter=libavfilter.8
    elif [[ ${major_ver} == 6 ]]; then
        avutil=libavutil.58
        avcodec=libavcodec.60
        avformat=libavformat.60
        avdevice=libavdevice.60
        avfilter=libavfilter.9
    else
        printf "Error: unexpected FFmpeg major version: %s\n"  ${major_ver}
        exit 1;
    fi
101

102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
    otool="/usr/bin/otool"
    # NOTE: miniconda has a version of otool and install_name_tool installed and we want
    #       to use the default sytem version instead of the miniconda version since the miniconda
    #       version can produce inconsistent results

    # Attempt to use /usr/bin/otool as our default otool
    if [[ ! -e ${otool} ]]; then
        otool="$(which otool)"
    fi
    install_name_tool="/usr/bin/install_name_tool"
    # Attempt to use /usr/bin/install_name_tool as our default install_name_tool
    if [[ ! -e ${install_name_tool} ]]; then
        install_name_tool="$(which install_name_tool)"
    fi

117
118
    # list up the paths to fix
    for lib in ${avcodec} ${avdevice} ${avfilter} ${avformat} ${avutil}; do
119
        ${otool} -l ${prefix}/lib/${lib}.dylib | grep -B2 ${prefix}
120
121
122
    done

    # Replace the hardcoded paths to @rpath
123
    ${install_name_tool} \
124
125
126
127
        -change ${prefix}/lib/${avutil}.dylib @rpath/${avutil}.dylib \
        -delete_rpath ${prefix}/lib \
        -id @rpath/${avcodec}.dylib \
        ${prefix}/lib/${avcodec}.dylib
128
    ${otool} -l ${prefix}/lib/${avcodec}.dylib | grep -B2 ${prefix}
129

130
    ${install_name_tool} \
131
132
133
134
135
136
        -change ${prefix}/lib/${avformat}.dylib @rpath/${avformat}.dylib \
        -change ${prefix}/lib/${avcodec}.dylib @rpath/${avcodec}.dylib \
        -change ${prefix}/lib/${avutil}.dylib @rpath/${avutil}.dylib \
        -delete_rpath ${prefix}/lib \
        -id @rpath/${avdevice}.dylib \
        ${prefix}/lib/${avdevice}.dylib
137
    ${otool} -l ${prefix}/lib/${avdevice}.dylib | grep -B2 ${prefix}
138

139
    ${install_name_tool} \
140
141
142
143
        -change ${prefix}/lib/${avutil}.dylib @rpath/${avutil}.dylib \
        -delete_rpath ${prefix}/lib \
        -id @rpath/${avfilter}.dylib \
        ${prefix}/lib/${avfilter}.dylib
144
    ${otool} -l ${prefix}/lib/${avfilter}.dylib | grep -B2 ${prefix}
145

146
    ${install_name_tool} \
147
148
149
150
151
        -change ${prefix}/lib/${avcodec}.dylib @rpath/${avcodec}.dylib \
        -change ${prefix}/lib/${avutil}.dylib @rpath/${avutil}.dylib \
        -delete_rpath ${prefix}/lib \
        -id @rpath/${avformat}.dylib \
        ${prefix}/lib/${avformat}.dylib
152
    ${otool} -l ${prefix}/lib/${avformat}.dylib | grep -B2 ${prefix}
153

154
    ${install_name_tool} \
155
156
157
        -delete_rpath ${prefix}/lib \
        -id @rpath/${avutil}.dylib \
        ${prefix}/lib/${avutil}.dylib
158
    ${otool} -l ${prefix}/lib/${avutil}.dylib | grep -B2 ${prefix}
159
fi