We discuss here how to use the MP4Box together with filters in GPAC.
As discussed here, the following features of MP4Box are now using the GPAC filter engine:
- Media importing
- Media exporting
At the time being, Media exporting (-raw) and encryption/decryption using -crypt / -decrypt are not configurable.
Customizing source parameters
It is possible to provide additional parameters to both source and destination during an import operations. Most MP4Box parameters are mapped to the filter engine and they have roughly the same names, but the opposite is not true.
MP4Box -add source.264:sopt:blocksize=20k:dopt:maxchunk=1M -new file.mp4
Any option for sources can be used, see the general filter documentation. The importer works by using the mp4 muxer filter in sink mode using the file specified as output for MP4Box. The filter graph can be seen by using the
MP4Box -add source.264:fgraph -new file.mp4 Filters connected: fin (src=source.aac:gpac:importer:index_dur=0:FID=1) (1) -(PID source.aac) rfadts (0x7fca97d0e430) --(PID source.aac) mp4mx (importer:file=0x7fca97e03cb0:SID=1#PID=1) (0x7fca97d080d0)
The filter statistics can be seen by using the
MP4Box -add source.aac:fstat -new file.mp4
Inserting a filter chain
It is possible to provide a filter chain to a source being imported in MP4Box.
MP4Box -add source.264@@reframer:sap=1 -new file.mp4
You can also play with encoding:
MP4Box -add source.264@@enc:c=avc:fintra=2 -new file.mp4
The above command will invoke an encoding filter chain in AVC|H264 format with a forced intra period of 2 seconds. The filter will be inserted between the source and the destination.
If your source is YUV (or PCM), you will have to insert source parameters using the
MP4Box -add source.yuv:sopt:size=320x240:fps=30000/1001:@@enc:c=avc:fintra=2 -new file.mp4
The above command will load the source file as a YUV 420 8 bits 320x240 @ 29.97 Hz.
You may ask yourself whether using MP4Box or gpac is more efficient for such an operation:
- When you add a single track using MP4Box to a new file, gpac and MP4Box are strictly equivalent.
- If you add several tracks in one shot in a new file, gpac will be more efficient as a single filter session will be used to import all tracks, whereas MP4Box uses one filter session per track to add (unless -newfs is set).
- The filter architecture does not support (for the moment) reading and writing in the same file, so if you need to add a track to an existing file, you must use MP4Box for that.
It is possible to provide a filter chain to each source being DASHed with MP4Box.
MP4Box -dash 1000 -profile live -out session.mpd source.mp4@@reframer:sap=1 source.mp4
The above command will invoke a reframer filter forwarding only IDR and discarding other frames, allowing to create a trick mode representation and a regular representation.
MP4Box -dash 2000 -profile live -out session.mpd source.mp4:@@enc:c=avc:fintra=2
The above command will invoke an encoding filter chain in AVC|H264 format with a forced intra period of 2 seconds.
MP4Box -dash 2000 -profile live -out session.mpd source.mp4:@@enc:c=avc:fintra=2@@cecrypt:cfile=drm.xml
The above command will invoke an encoding filter chain in AVC|H264 format with a forced intra period of 2 seconds, followed by an encryption driven by the file
The filter chain will be run at each invocation of MP4Box. Therefore it is not recommended to use complex chains (eg encoding or encrypting) and -dash-ctx or -dash-live together, as the content would be processed (encoded, encrypted) multiple times, at least up to the current DASH time.
You may ask yourself whether using MP4Box or gpac is more efficient for such an operation. If you don't use
-add options, MP4Box and gpac are strictly equivalent, a single filter session is used by both to dash the content.
Note: sources to MP4Box for dashing are no longer restricted to MP4 files. The dasher filter will enforce MP4 output but processes any source:
MP4Box -dash 2000 -profile live -out session.mpd source.264 source.aac
The above command will invoke create a DASH session from non packaged AVC|H264 and AAC sources, using ISOBMFF as output format.
MP4Box -dash 2000 -profile live -out session.mpd:m2ts source.264 source.aac
The above command will invoke create a DASH session from non packaged AVC|H264 and AAC sources, using MPEG-2 TS as output format.