Skip to content

Commit 1035e35

Browse files
committedJul 31, 2024
initial commit
0 parents  commit 1035e35

16 files changed

+1620
-0
lines changed
 

‎.gitignore

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist-ssr
12+
*.local
13+
14+
# Editor directories and files
15+
.vscode/*
16+
!.vscode/extensions.json
17+
.idea
18+
.DS_Store
19+
*.suo
20+
*.ntvs*
21+
*.njsproj
22+
*.sln
23+
*.sw?
24+
.vscode
25+
.github/*
26+
27+
# wasm build file
28+
src/lib/*.js
29+
src/lib/*.wasm

‎.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "lib/FFmpeg"]
2+
path = lib/FFmpeg
3+
url = https://github.com/FFmpeg/FFmpeg.git

‎LICENSE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
MIT License
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.

‎Makefile

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
FFMPEG_CONFIGURE_ARGS = \
2+
--target-os=none \
3+
--arch=x86_32 \
4+
--cc=emcc \
5+
--ranlib=emranlib \
6+
--disable-all \
7+
--disable-asm \
8+
--enable-avcodec \
9+
--enable-avformat \
10+
--enable-protocol=file
11+
12+
DEMUX_ARGS = \
13+
--enable-demuxer=mov,mp4,avi,matroska,webm
14+
15+
WEB_DEMUXER_ARGS = \
16+
emcc ./lib/web-demuxer/*.c \
17+
-I./lib/FFmpeg \
18+
-L./lib/FFmpeg/libavformat -lavformat \
19+
-L./lib/FFmpeg/libavutil -lavutil \
20+
-L./lib/FFmpeg/libavcodec -lavcodec \
21+
--post-js ./lib/web-demuxer/post.js \
22+
-lworkerfs.js \
23+
-O3 \
24+
-s EXPORT_ES6=1 \
25+
-s INVOKE_RUN=0 \
26+
-s ENVIRONMENT=worker \
27+
-s EXPORTED_RUNTIME_METHODS=cwrap,getValue,UTF8ToString \
28+
-s EXPORTED_FUNCTIONS=_free \
29+
-s ASYNCIFY \
30+
-s WASM_BIGINT \
31+
-s ALLOW_MEMORY_GROWTH=1
32+
33+
clean:
34+
cd lib/FFmpeg && \
35+
make clean && \
36+
make distclean
37+
38+
ffmpeg-lib:
39+
cd lib/FFmpeg && \
40+
emconfigure ./configure $(FFMPEG_CONFIGURE_ARGS) $(DEMUX_ARGS) && \
41+
emmake make
42+
43+
web-demuxer:
44+
$(WEB_DEMUXER_ARGS) -o ./dist/ffmpeg.js

‎README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# FFmpeg WASM DEMUXER
2+
FFmpeg LGPL v2.1 build steps for generating a wasm module using emscripten.
3+
4+
### Steps to Reproduce
5+
First execute `npm run dev:docker:arm64` to start a docker container with docker compose.
6+
7+
Next, execute `npm run build:wasm` to build the demuxer for the specified formats.

‎compose.yaml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
services:
2+
dev-web-demuxer-arm64:
3+
container_name: web-demuxer
4+
image: emscripten/emsdk:3.1.60-arm64
5+
working_dir: /app
6+
volumes:
7+
- ./:/app
8+
stdin_open: true
9+
tty: true
10+
command: ["/bin/bash"]

‎dist/ffmpeg.js

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/ffmpeg.wasm

921 KB
Binary file not shown.

‎lib/web-demuxer/audio_codec_string.c

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#include "audio_codec_string.h"
2+
#include "libavformat/internal.h"
3+
#include "libavutil/avstring.h"
4+
#include "libavutil/intreadwrite.h"
5+
6+
// sync with ff_mp4_obj_type in isom.c
7+
const AVCodecTag _ff_mp4_obj_type[] = {
8+
{ AV_CODEC_ID_MOV_TEXT , 0x08 },
9+
{ AV_CODEC_ID_MPEG4 , 0x20 },
10+
{ AV_CODEC_ID_H264 , 0x21 },
11+
{ AV_CODEC_ID_HEVC , 0x23 },
12+
{ AV_CODEC_ID_AAC , 0x40 },
13+
{ AV_CODEC_ID_MP4ALS , 0x40 }, /* 14496-3 ALS */
14+
{ AV_CODEC_ID_MPEG2VIDEO , 0x61 }, /* MPEG-2 Main */
15+
{ AV_CODEC_ID_MPEG2VIDEO , 0x60 }, /* MPEG-2 Simple */
16+
{ AV_CODEC_ID_MPEG2VIDEO , 0x62 }, /* MPEG-2 SNR */
17+
{ AV_CODEC_ID_MPEG2VIDEO , 0x63 }, /* MPEG-2 Spatial */
18+
{ AV_CODEC_ID_MPEG2VIDEO , 0x64 }, /* MPEG-2 High */
19+
{ AV_CODEC_ID_MPEG2VIDEO , 0x65 }, /* MPEG-2 422 */
20+
{ AV_CODEC_ID_AAC , 0x66 }, /* MPEG-2 AAC Main */
21+
{ AV_CODEC_ID_AAC , 0x67 }, /* MPEG-2 AAC Low */
22+
{ AV_CODEC_ID_AAC , 0x68 }, /* MPEG-2 AAC SSR */
23+
{ AV_CODEC_ID_MP3 , 0x69 }, /* 13818-3 */
24+
{ AV_CODEC_ID_MP2 , 0x69 }, /* 11172-3 */
25+
{ AV_CODEC_ID_MPEG1VIDEO , 0x6A }, /* 11172-2 */
26+
{ AV_CODEC_ID_MP3 , 0x6B }, /* 11172-3 */
27+
{ AV_CODEC_ID_MJPEG , 0x6C }, /* 10918-1 */
28+
{ AV_CODEC_ID_PNG , 0x6D },
29+
{ AV_CODEC_ID_JPEG2000 , 0x6E }, /* 15444-1 */
30+
{ AV_CODEC_ID_VC1 , 0xA3 },
31+
{ AV_CODEC_ID_DIRAC , 0xA4 },
32+
{ AV_CODEC_ID_AC3 , 0xA5 },
33+
{ AV_CODEC_ID_EAC3 , 0xA6 },
34+
{ AV_CODEC_ID_DTS , 0xA9 }, /* mp4ra.org */
35+
{ AV_CODEC_ID_OPUS , 0xAD }, /* mp4ra.org */
36+
{ AV_CODEC_ID_VP9 , 0xB1 }, /* mp4ra.org */
37+
{ AV_CODEC_ID_TSCC2 , 0xD0 }, /* nonstandard, camtasia uses it */
38+
{ AV_CODEC_ID_EVRC , 0xD1 }, /* nonstandard, pvAuthor uses it */
39+
{ AV_CODEC_ID_VORBIS , 0xDD }, /* nonstandard, gpac uses it */
40+
{ AV_CODEC_ID_DVD_SUBTITLE, 0xE0 }, /* nonstandard, see unsupported-embedded-subs-2.mp4 */
41+
{ AV_CODEC_ID_QCELP , 0xE1 },
42+
{ AV_CODEC_ID_MPEG4SYSTEMS, 0x01 },
43+
{ AV_CODEC_ID_MPEG4SYSTEMS, 0x02 },
44+
{ AV_CODEC_ID_NONE , 0 },
45+
};
46+
47+
void set_aac_codec_string(char *str, size_t str_size, AVCodecParameters *par)
48+
{
49+
50+
av_strlcpy(str, "mp4a", str_size);
51+
52+
const AVCodecTag *tags[1] = { NULL };
53+
uint32_t oti;
54+
55+
tags[0] = _ff_mp4_obj_type;
56+
oti = av_codec_get_tag(tags, par->codec_id);
57+
58+
if (oti)
59+
av_strlcatf(str, str_size, ".%02" PRIx32, oti);
60+
else
61+
return;
62+
63+
if (par->extradata_size >= 2)
64+
{
65+
int aot = par->extradata[0] >> 3;
66+
if (aot == 31)
67+
aot = ((AV_RB16(par->extradata) >> 5) & 0x3f) + 32;
68+
av_strlcatf(str, str_size, ".%d", aot);
69+
}
70+
}
71+
72+
void set_audio_codec_string(char *str, size_t str_size, AVCodecParameters *par)
73+
{
74+
switch (par->codec_id)
75+
{
76+
case AV_CODEC_ID_FLAC:
77+
av_strlcpy(str, "flac", str_size);
78+
break;
79+
case AV_CODEC_ID_MP3:
80+
av_strlcpy(str, "mp3", str_size);
81+
break;
82+
case AV_CODEC_ID_AAC:
83+
set_aac_codec_string(str, str_size, par);
84+
break;
85+
case AV_CODEC_ID_OPUS:
86+
av_strlcpy(str, "opus", str_size);
87+
break;
88+
case AV_CODEC_ID_VORBIS:
89+
av_strlcpy(str, "vorbis", str_size);
90+
break;
91+
case AV_CODEC_ID_PCM_MULAW:
92+
av_strlcpy(str, "ulaw", str_size);
93+
break;
94+
case AV_CODEC_ID_PCM_ALAW:
95+
av_strlcpy(str, "alaw", str_size);
96+
break;
97+
case AV_CODEC_ID_PCM_U8:
98+
av_strlcpy(str, "pcm-u8", str_size);
99+
break;
100+
case AV_CODEC_ID_PCM_S16LE:
101+
case AV_CODEC_ID_PCM_S16BE:
102+
av_strlcpy(str, "pcm-s16", str_size);
103+
break;
104+
case AV_CODEC_ID_PCM_S24LE:
105+
case AV_CODEC_ID_PCM_S24BE:
106+
av_strlcpy(str, "pcm-s24", str_size);
107+
break;
108+
case AV_CODEC_ID_PCM_S32LE:
109+
case AV_CODEC_ID_PCM_S32BE:
110+
av_strlcpy(str, "pcm-s32", str_size);
111+
break;
112+
case AV_CODEC_ID_PCM_F32LE:
113+
case AV_CODEC_ID_PCM_F32BE:
114+
av_strlcpy(str, "pcm-f32", str_size);
115+
break;
116+
default:
117+
av_strlcpy(str, "undf", str_size);
118+
break;
119+
}
120+
}

‎lib/web-demuxer/audio_codec_string.h

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "libavformat/avformat.h"
2+
3+
void set_audio_codec_string(char *str, size_t str_size, AVCodecParameters *par);

0 commit comments

Comments
 (0)
Please sign in to comment.