The Purpose of zapcvt

While zapdvb handles tuning and extracts MPEG data from DVB streams, the zapcvt knows about the MPEG internals and can be used to manipulate the data. It can split or concatenate MPEG2 files, it can edit MPEG2 video or audio files and it can run a viewer or player. It can also extract the audio or video stream of a PES MPEG2 file. The program can only handle MPEG2 video and audio, but conversion to or from a historic format named PVA is also supported.

When the -r option is given zapcvt does rewrite the MPEG headers and the time-stamps and can eventually be used to repair files that some players (notably xine) would not play otherwise.

Together with KDE it can be used as application launcher that can easily start your favorate player while keeping the configuration arguments for the players in an easily accessible configuration file (see Usage Examples).

Command Line Options

The general call syntax is:

zapcvt [options] [file...]

The optional file... argument(s) can be a single input file or a list of input files. In the later case all input files are treated as if they had been previously concatenated, e.g. zapcvt handles the list as a virtual input file. If no file is given data is read from stdin.

The following table lists the available options. Note: optional parameters like [mode] must follow the option letter without a space!

.
Options that select an Action
-a Output data in PVA format.
-m Output data in MPEG2 format.
-n (for: no conversion) Output data in PVA or MPEG format, depending on input.
-d [view] Launch a player or viewer program and pipe the input data to it. The programs and their default arguments are configurable via the [zapcvt] section of the config file. The following numeric values can be used (inside the brackets the corresponding config file entry is given):
1 (cmdhigh=)
High Quality video playback (deinterlaced, full-screen)
2 (cmdprev=)
Preview mode (not deinterlaced)
3 (cmdfast=)
Fast Preview mode (no sound)
4 (cmdplay=)
Play sound only (-d4, implies -i2)
5
Run what the -x option specifies (this is implied by option -x)
6 (cmdview=)
Start a video player and pass the input file path to it if possible, otherwise pipe the data. One possible use of -d6 is the application launcher example.
7 (cmdvdvd=)
Start a DVD player and pass the remaining input arguments to it (consider using -- if the arguments are options).
-h Display a brief usage summary and quit.
-i [mode] Extract video or audio data as an ES (Elementary Stream). A value of 1 extracts video and 2 extracts audio data from a PES (Packetized Elementary Stream). If the input is already an ES this command has no effect but is used to flag an action. Note: zapdvb records TV channels as PES and radio channels as ES.
-x cmd Runs a command (see the comments in zapdvb.conf concerning quoting and arguments). Example (the quotes are required): -x "mpg123 -q -" .
Editing Commands
-G pos (for: GOTO) Advance to a given position in input (measured in %, example: 21.123). This option is used together with -U to specify a clipping region. All -g or -u options will be relative to the range specified by -G and -U. If -U is used but -G is omitted, the clipping region starts at 0%.
-U pos (for: UNTIL) Stop at a given position in input, see -G. If -G is used but -U is omitted, the clipping region ends at 100%.
-g (for: goto) Advance to a given position in input (measured in %, example: 21.123). This option is typically followed by -u. A list of pairs of -g and -u options can be used to edit an audio or video file. If the initial -g is omitted, -g0 is implied. All video edit commands will advance in input until a GOP (group of pictures) mark is found - usually a GOP occurs in input every 400ms. When editing an mp2 audio ES (Elementary Stream) input advances until the next frame header.
-r [mode] Controls the rewriting of MPEG headers and time-stamps. A value of 0 disables header rewriting and copies the input headers verbatim to the output. Using 1 will rewrite the headers but will literally copy the time-stamps, and using 2 (the default) rewrites headers and time-stamps. When rewriting time-stamps the file will start with pts=0, and time gaps in the input will be removed (pts stands for Presentation Time Stamp). When header rewriting is enabled no dts info will be written (dts stands for Display Time Stamp).
-u (for: until) Stop at a given position in input, see -g. If the final -u is omitted, -u100 is implied.
View Aspects and Tagging
-t list Lists the tag information for the current file. Returns EXIT_OK on success and EXIT_INPUT if no tag information was found.
-t remove Removes the tag information for the current file. If a separate tag file is found, the tag file gets deleted even if the tag data was read from the MPEG file.
-t check Checks if there is tag information for the current file. Returns EXIT_OK on success and EXIT_INPUT if no tag information was found. The tag information can be contained in the MPEG file or can be in a separate tag file.
-t val... A semicolon delimited list of key/value pairs replaces the current tag information (if any). The option will never create a new tag file, but if a tag file exists and the MPEG file does not contain tag data the information will be stored in that file. If the MPEG file already contains tag information this data will get replaced and an existing tag file will be deleted. If no existing tag data is found the new tag information will be stored in the MPEG file.
-V mode Select a view mode. See the [views] section of the zapdvb.conf file
Miscellaneous
-c file Explicitly specify a configuration file. Usually zapdvb and zapcvt share a common configuration file. By default zapcvt first tries to find a local configuration under $HOME/.zapdvb and if this does not exist it tries to locate a global zapdvb.conf. The global configuration is searched in the installation folder (sym links get dereferenced), /usr/local/bin, /usr/local/etc and /etc. Only the [zapcvt] section is read by zapcvt.
-h Display a brief usage summary and quit.
-o output Write output to a file specified by output. If the option is not used output is written to stdout. Using -o implies -n if no other action is specified. If the output specifies no file name extension, .mpg is appended for PES video, .pva is appended for PVA video and .mp2 for audio output.
-s MByte Split the input into several output files with a maximum size of MByte MByte. The -o option must be used to make this work, and a three digit sequence number will be inserted before the file name extension.
-v [level]    Progress reporting and debug output. The levels are: 0 quiet, 1 progress report (default), 2 debug output.

Usage Examples

Application Launcher (also nice with KDE) ...
# For KDE you would associate these commands with filename extensions or
# .desktop files. The advantage is that the player configuration is read
# from the zapdvb configuration file (zapdvb.conf or $HOME/.zapdvb) ...

zapcvt -d6 videofile.mpg          # Launch player for high quality
zapcvt -d6 videofile.pva          # convert PVA to MPEG2, pipe to player
zapcvt -d7 dvd://1                # Launch DVD player (passing arguments)
Editing Video and Audio Data ...
zapcvt -d -g50 videofile.mpg      # play video, start at 50% of file
zapcvt -d4 -g10 -u60 mysong.mp2   # play audio from 10% to 60% of file

# multiple pairs of -g und -u can be combined to remove TV advertising...

zapcvt -g3.1 -u21.21 -g27.2 -u43.11 -g48.4 -u89.4 -r -v -o out.mpg tvin.mpg
zapcvt -i -g5 -u15 -o mysong.mp2 radioin.mp2
zapcvt -i -g5 -u15 -x "lame --mp2input - mysong.mp3" radioin.mp2
Extracting Video or Audio Data ...
zapcvt -d4 audiofile.mp2          # Play. Only MPEG Layer2 audio will work!
zapcvt -d4 videofile.mpg          # Extract and play audio from PES video

# Extracting data as ES from a PES MPEG video ...

zapcvt -i2 -v videofile.mpg -o musicout.mp2     # get audio ES
zapcvt -i1 -v videofile.mpg -o videoout.mpg     # get video ES
Splitting, Joining and Repairing Files ...
# split input into multiple files below the 2GByte limit ...

zapcvt -s 2047 binInput.mpg -m smallout         # -m creates .mpg files
zapcvt -s 2047 binInput.mpg smallout.mpg        # needs .mpg without -m

# multiple input files can be combined ...

zapcvt -v capture*.mpg -o joined.mpg            # for video
zapcvt -v -i capture*.mp2 -o joined.mp2         # for audio (needs -i)

# rewrite MPEG headers to "repair" a video ...

zapcvt -v -r2 myClip1.mpg myClip2.mpg -o myClips.mpg

Some Remarks on MPEG Video (and Audio) Editing

Initially zapcvt was supposed to be a MPEG2/PVA converter because a WIN32 program named PVACut was used as video editor (running under Wine). This was cumbersome because it was to slow for all-days use. The current video edit support was born by curiosity (can I make it faster?) - and it works fast!

Unfortunately it is not trivial to cut MPEG2 videos. There are three problems (1) no complete public documentation of MPEG2, (2) simple programs like zapcvt (and PVACut) work only on GOP (group of pictures) boundaries and (3) audio follows video with a delay of 100ms.

Problem 1: try to find information searching the Web, look into the code of other authors. Make it as simple as possible, test with Mplayer and Xine.

Problem 2: one future enhancement could be to patch the GOP before the cut to be shorter. Making the GOP behind the cut shorter would require re-encoding of MPEG data. The later is currently out of reach for zapcvt, although a public domain implementation of this algorithm is available.

Problem 3: this requires reordering of packets before writing them. Until now (version 0.30) zapcvt does not implement this, which could be seen as a bug. Fortunately the players (Mplayer and Xine) seem to handle this for us. Future versions of zapcvt might implement reordering (using -r3) and should thus be able to "repair" files written by the current version.

Audio Editing: was added to support the merging of zapdvb audio capture data. The algorithm is primitive and works on frames without looking into the frame data. This sometimes introduces a "click" noise at the cut position. No enhancements are planned.