Skip to content

Commit 600f1d8

Browse files
authored
Merge pull request #776 from izogfif/main
Expose ffprobe command from ffmpeg.wasm
2 parents 7fe8dff + bd903bb commit 600f1d8

File tree

8 files changed

+147
-17
lines changed

8 files changed

+147
-17
lines changed

build/ffmpeg-wasm.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ CONF_FLAGS=(
3030
-lswscale
3131
-Wno-deprecated-declarations
3232
$LDFLAGS
33+
-sENVIRONMENT=worker
3334
-sWASM_BIGINT # enable big int support
3435
-sUSE_SDL=2 # use emscripten SDL2 lib port
3536
-sMODULARIZE # modularized to use as a library
@@ -49,6 +50,7 @@ CONF_FLAGS=(
4950
src/fftools/ffmpeg_mux.c
5051
src/fftools/ffmpeg_opt.c
5152
src/fftools/opt_common.c
53+
src/fftools/ffprobe.c
5254
)
5355

5456
emcc "${CONF_FLAGS[@]}" $@

packages/ffmpeg/src/classes.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export class FFmpeg {
6262
case FFMessageType.MOUNT:
6363
case FFMessageType.UNMOUNT:
6464
case FFMessageType.EXEC:
65+
case FFMessageType.FFPROBE:
6566
case FFMessageType.WRITE_FILE:
6667
case FFMessageType.READ_FILE:
6768
case FFMessageType.DELETE_FILE:
@@ -249,6 +250,42 @@ export class FFmpeg {
249250
signal
250251
) as Promise<number>;
251252

253+
/**
254+
* Execute ffprobe command.
255+
*
256+
* @example
257+
* ```ts
258+
* const ffmpeg = new FFmpeg();
259+
* await ffmpeg.load();
260+
* await ffmpeg.writeFile("video.avi", ...);
261+
* // Getting duration of a video in seconds: ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 video.avi -o output.txt
262+
* await ffmpeg.ffprobe(["-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", "video.avi", "-o", "output.txt"]);
263+
* const data = ffmpeg.readFile("output.txt");
264+
* ```
265+
*
266+
* @returns `0` if no error, `!= 0` if timeout (1) or error.
267+
* @category FFmpeg
268+
*/
269+
public ffprobe = (
270+
/** ffprobe command line args */
271+
args: string[],
272+
/**
273+
* milliseconds to wait before stopping the command execution.
274+
*
275+
* @defaultValue -1
276+
*/
277+
timeout = -1,
278+
{ signal }: FFMessageOptions = {}
279+
): Promise<number> =>
280+
this.#send(
281+
{
282+
type: FFMessageType.FFPROBE,
283+
data: { args, timeout },
284+
},
285+
undefined,
286+
signal
287+
) as Promise<number>;
288+
252289
/**
253290
* Terminate all ongoing API calls and terminate web worker.
254291
* `FFmpeg.load()` must be called again before calling any other APIs.

packages/ffmpeg/src/const.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const CORE_URL = `https://unpkg.com/@ffmpeg/core@${CORE_VERSION}/dist/umd
77
export enum FFMessageType {
88
LOAD = "LOAD",
99
EXEC = "EXEC",
10+
FFPROBE = "FFPROBE",
1011
WRITE_FILE = "WRITE_FILE",
1112
READ_FILE = "READ_FILE",
1213
DELETE_FILE = "DELETE_FILE",

packages/ffmpeg/src/worker.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ const exec = ({ args, timeout = -1 }: FFMessageExecData): ExitCode => {
100100
return ret;
101101
};
102102

103+
const ffprobe = ({ args, timeout = -1 }: FFMessageExecData): ExitCode => {
104+
ffmpeg.setTimeout(timeout);
105+
ffmpeg.ffprobe(...args);
106+
const ret = ffmpeg.ret;
107+
ffmpeg.reset();
108+
return ret;
109+
};
110+
103111
const writeFile = ({ path, data }: FFMessageWriteFileData): OK => {
104112
ffmpeg.FS.writeFile(path, data);
105113
return true;
@@ -170,6 +178,9 @@ self.onmessage = async ({
170178
case FFMessageType.EXEC:
171179
data = exec(_data as FFMessageExecData);
172180
break;
181+
case FFMessageType.FFPROBE:
182+
data = ffprobe(_data as FFMessageExecData);
183+
break;
173184
case FFMessageType.WRITE_FILE:
174185
data = writeFile(_data as FFMessageWriteFileData);
175186
break;

packages/types/types/index.d.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,28 @@ export interface Stat {
3939
blocks: number;
4040
}
4141

42-
export interface FSFilesystemWORKERFS {
43-
44-
}
42+
export interface FSFilesystemWORKERFS {}
4543

46-
export interface FSFilesystemMEMFS {
47-
48-
}
44+
export interface FSFilesystemMEMFS {}
4945

5046
export interface FSFilesystems {
5147
WORKERFS: FSFilesystemWORKERFS;
5248
MEMFS: FSFilesystemMEMFS;
5349
}
5450

55-
export type FSFilesystem =
56-
| FSFilesystemWORKERFS
57-
| FSFilesystemMEMFS;
51+
export type FSFilesystem = FSFilesystemWORKERFS | FSFilesystemMEMFS;
52+
53+
export interface OptionReadFile {
54+
encoding: string;
55+
}
56+
57+
export interface WorkerFSMountConfig {
58+
blobs?: {
59+
name: string;
60+
data: Blob;
61+
}[];
62+
files?: File[];
63+
}
5864

5965
/**
6066
* Functions to interact with Emscripten FS library.
@@ -75,7 +81,11 @@ export interface FS {
7581
isFile: (mode: number) => boolean;
7682
/** mode is a numeric notation of permission, @see [Numeric Notation](https://en.wikipedia.org/wiki/File-system_permissions#Numeric_notation) */
7783
isDir: (mode: number) => boolean;
78-
mount: (fileSystemType: FSFilesystem, data: WorkerFSMountConfig, path: string) => void;
84+
mount: (
85+
fileSystemType: FSFilesystem,
86+
data: WorkerFSMountConfig,
87+
path: string
88+
) => void;
7989
unmount: (path: string) => void;
8090
filesystems: FSFilesystems;
8191
}
@@ -115,6 +125,7 @@ export interface FFmpegCoreModule {
115125
mainScriptUrlOrBlob: string;
116126

117127
exec: (...args: string[]) => number;
128+
ffprobe: (...args: string[]) => number;
118129
reset: () => void;
119130
setLogger: (logger: (log: Log) => void) => void;
120131
setTimeout: (timeout: number) => void;

src/bind/ffmpeg/bind.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
const NULL = 0;
66
const SIZE_I32 = Uint32Array.BYTES_PER_ELEMENT;
77
const DEFAULT_ARGS = ["./ffmpeg", "-nostdin", "-y"];
8+
const DEFAULT_ARGS_FFPROBE = ["./ffprobe"];
89

910
Module["NULL"] = NULL;
1011
Module["SIZE_I32"] = SIZE_I32;
1112
Module["DEFAULT_ARGS"] = DEFAULT_ARGS;
13+
Module["DEFAULT_ARGS_FFPROBE"] = DEFAULT_ARGS_FFPROBE;
1214

1315
/**
1416
* Variables
@@ -62,6 +64,18 @@ function exec(..._args) {
6264
return Module["ret"];
6365
}
6466

67+
function ffprobe(..._args) {
68+
const args = [...Module["DEFAULT_ARGS_FFPROBE"], ..._args];
69+
try {
70+
Module["_ffprobe"](args.length, stringsToPtr(args));
71+
} catch (e) {
72+
if (!e.message.startsWith("Aborted")) {
73+
throw e;
74+
}
75+
}
76+
return Module["ret"];
77+
}
78+
6579
function setLogger(logger) {
6680
Module["logger"] = logger;
6781
}
@@ -121,6 +135,7 @@ Module["printErr"] = printErr;
121135
Module["locateFile"] = _locateFile;
122136

123137
Module["exec"] = exec;
138+
Module["ffprobe"] = ffprobe;
124139
Module["setLogger"] = setLogger;
125140
Module["setTimeout"] = setTimeout;
126141
Module["setProgress"] = setProgress;

src/bind/ffmpeg/export.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
const EXPORTED_FUNCTIONS = ["_ffmpeg", "_abort", "_malloc"];
1+
const EXPORTED_FUNCTIONS = ["_ffmpeg", "_abort", "_malloc", "_ffprobe"];
22

33
console.log(EXPORTED_FUNCTIONS.join(","));

src/fftools/ffprobe.c

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ typedef struct InputFile {
9090
int nb_streams;
9191
} InputFile;
9292

93-
const char program_name[] = "ffprobe";
94-
const int program_birth_year = 2007;
93+
const char program_name_ffprobe[] = "ffprobe";
94+
const int program_birth_year_ffprobe = 2007;
9595

9696
static int do_bitexact = 0;
9797
static int do_count_frames = 0;
@@ -382,6 +382,58 @@ static void ffprobe_cleanup(int ret)
382382
#if HAVE_THREADS
383383
pthread_mutex_destroy(&log_mutex);
384384
#endif
385+
386+
do_bitexact = 0;
387+
do_count_frames = 0;
388+
do_count_packets = 0;
389+
do_read_frames = 0;
390+
do_read_packets = 0;
391+
do_show_chapters = 0;
392+
do_show_error = 0;
393+
do_show_format = 0;
394+
do_show_frames = 0;
395+
do_show_packets = 0;
396+
do_show_programs = 0;
397+
do_show_streams = 0;
398+
do_show_stream_disposition = 0;
399+
do_show_data = 0;
400+
do_show_program_version = 0;
401+
do_show_library_versions = 0;
402+
do_show_pixel_formats = 0;
403+
do_show_pixel_format_flags = 0;
404+
do_show_pixel_format_components = 0;
405+
do_show_log = 0;
406+
do_show_chapter_tags = 0;
407+
do_show_format_tags = 0;
408+
do_show_frame_tags = 0;
409+
do_show_program_tags = 0;
410+
do_show_stream_tags = 0;
411+
do_show_packet_tags = 0;
412+
show_value_unit = 0;
413+
use_value_prefix = 0;
414+
use_byte_value_binary_prefix = 0;
415+
use_value_sexagesimal_format = 0;
416+
show_private_data = 1;
417+
show_optional_fields = SHOW_OPTIONAL_FIELDS_AUTO;
418+
print_format = NULL;
419+
stream_specifier = NULL;
420+
show_data_hash = NULL;
421+
read_intervals = NULL;
422+
read_intervals_nb = 0;
423+
find_stream_info = 1;
424+
input_filename = NULL;
425+
print_input_filename = NULL;
426+
iformat = NULL;
427+
output_filename = NULL;
428+
hash = NULL;
429+
nb_streams = 0;
430+
nb_streams_packets = NULL;
431+
nb_streams_frames = NULL;
432+
selected_streams = NULL;
433+
log_buffer = NULL;
434+
log_buffer_size = 0;
435+
436+
av_log(NULL, AV_LOG_DEBUG, "FFprobe: Cleanup done.\n");
385437
}
386438

387439
struct unit_value {
@@ -3491,7 +3543,7 @@ static int probe_file(WriterContext *wctx, const char *filename,
34913543
static void show_usage(void)
34923544
{
34933545
av_log(NULL, AV_LOG_INFO, "Simple multimedia streams analyzer\n");
3494-
av_log(NULL, AV_LOG_INFO, "usage: %s [OPTIONS] INPUT_FILE\n", program_name);
3546+
av_log(NULL, AV_LOG_INFO, "usage: %s [OPTIONS] INPUT_FILE\n", program_name_ffprobe);
34953547
av_log(NULL, AV_LOG_INFO, "\n");
34963548
}
34973549

@@ -3503,7 +3555,7 @@ static void ffprobe_show_program_version(WriterContext *w)
35033555
writer_print_section_header(w, SECTION_ID_PROGRAM_VERSION);
35043556
print_str("version", FFMPEG_VERSION);
35053557
print_fmt("copyright", "Copyright (c) %d-%d the FFmpeg developers",
3506-
program_birth_year, CONFIG_THIS_YEAR);
3558+
program_birth_year_ffprobe, CONFIG_THIS_YEAR);
35073559
print_str("compiler_ident", CC_IDENT);
35083560
print_str("configuration", FFMPEG_CONFIGURATION);
35093561
writer_print_section_footer(w);
@@ -3740,7 +3792,7 @@ static int opt_print_filename(void *optctx, const char *opt, const char *arg)
37403792
return 0;
37413793
}
37423794

3743-
void show_help_default(const char *opt, const char *arg)
3795+
void show_help_default_ffprobe(const char *opt, const char *arg)
37443796
{
37453797
av_log_set_callback(log_callback_help);
37463798
show_usage();
@@ -4037,6 +4089,7 @@ int ffprobe(int argc, char **argv)
40374089
}
40384090
#endif
40394091
av_log_set_flags(AV_LOG_SKIP_REPEATED);
4092+
ffprobe_cleanup(0);
40404093
register_exit(ffprobe_cleanup);
40414094

40424095
options = real_options;
@@ -4142,7 +4195,7 @@ int ffprobe(int argc, char **argv)
41424195
(!do_show_program_version && !do_show_library_versions && !do_show_pixel_formats))) {
41434196
show_usage();
41444197
av_log(NULL, AV_LOG_ERROR, "You have to specify one input file.\n");
4145-
av_log(NULL, AV_LOG_ERROR, "Use -h to get full help or, even better, run 'man %s'.\n", program_name);
4198+
av_log(NULL, AV_LOG_ERROR, "Use -h to get full help or, even better, run 'man %s'.\n", program_name_ffprobe);
41464199
ret = AVERROR(EINVAL);
41474200
} else if (input_filename) {
41484201
ret = probe_file(wctx, input_filename, print_input_filename);

0 commit comments

Comments
 (0)