mpeg2encode

kroc 01 Apr 2004 - mpeg2encode is an utility to encode mpeg video. It can be downloaded from: http://www.mpeg.org/MPEG/MSSG/

To build a mpeg with Tk and mpeg2encode follow this:

Records each frame in a directory in ppm image format. You must use a common basename (video here) followed by _XXXX.ppm where XXXX is the frame number. All frames must have the same size (320x200 pixels here). Example:

 tkimagename write video_0001.ppm -format ppm

As we will use 24 frames per second, you'll need to record a lot of ppm. Once all your frames are recorded, write a video.par_mpg in the same directory with this content:

 MPEG-1 stream 24 frames/sec
 /path/to/video_%04d   /* name of source files */
 -         /* name of reconstructed images ("-": don't store) */
 -         /* name of intra quant matrix file     ("-": default matrix) */
 -         /* name of non intra quant matrix file ("-": default matrix) */
 -         /* name of statistics file ("-": stdout ) */
 2         /* input picture file format: 0=*.Y,*.U,*.V, 1=*.yuv, 2=*.ppm */
 406       /* number of frames */
 1         /* number of first frame */
 00:00:00:00 /* timecode of first frame */
 12        /* N (# of frames in GOP) */
 3         /* M (I/P frame distance) */
 1         /* ISO/IEC 11172-2 stream */
 0         /* 0:frame pictures, 1:field pictures */
 320       /* horizontal_size */
 200       /* vertical_size */
 2         /* aspect_ratio_information 1=square pel, 2=4:3, 3=16:9, 4=2.11:1 */
 2         /* frame_rate_code 1=23.976, 2=24, 3=25, 4=29.97, 5=30 frames/sec. */
 3000000.0 /* bit_rate (bits/s) */
 112       /* vbv_buffer_size (in multiples of 16 kbit) */
 0         /* low_delay  */
 0         /* constrained_parameters_flag */
 4         /* Profile ID: Simple = 5, Main = 4, SNR = 3, Spatial = 2, High = 1 */
 8         /* Level ID:   Low = 10, Main = 8, High 1440 = 6, High = 4          */
 0         /* progressive_sequence */
 1         /* chroma_format: 1=4:2:0, 2=4:2:2, 3=4:4:4 */
 1         /* video_format: 0=comp., 1=PAL, 2=NTSC, 3=SECAM, 4=MAC, 5=unspec. */
 5         /* color_primaries */
 5         /* transfer_characteristics */
 5         /* matrix_coefficients */
 320       /* display_horizontal_size */
 200       /* display_vertical_size */
 0         /* intra_dc_precision (0: 8 bit, 1: 9 bit, 2: 10 bit, 3: 11 bit */
 1         /* top_field_first */
 0 0 0     /* frame_pred_frame_dct (I P B) */
 0 0 0     /* concealment_motion_vectors (I P B) */
 1 1 1     /* q_scale_type  (I P B) */
 1 0 0     /* intra_vlc_format (I P B)*/
 0 0 0     /* alternate_scan (I P B) */
 0         /* repeat_first_field */
 0         /* progressive_frame */
 0         /* P distance between complete intra slice refresh */
 0         /* rate control: r (reaction parameter) */
 0         /* rate control: avg_act (initial average activity) */
 0         /* rate control: Xi (initial I frame global complexity measure) */
 0         /* rate control: Xp (initial P frame global complexity measure) */
 0         /* rate control: Xb (initial B frame global complexity measure) */
 0         /* rate control: d0i (initial I frame virtual buffer fullness) */
 0         /* rate control: d0p (initial P frame virtual buffer fullness) */
 0         /* rate control: d0b (initial B frame virtual buffer fullness) */
 2 2 11 11 /* P:  forw_hor_f_code forw_vert_f_code search_width/height */
 1 1 3  3  /* B1: forw_hor_f_code forw_vert_f_code search_width/height */
 1 1 7  7  /* B1: back_hor_f_code back_vert_f_code search_width/height */
 1 1 7  7  /* B2: forw_hor_f_code forw_vert_f_code search_width/height */
 1 1 3  3  /* B2: back_hor_f_code back_vert_f_code search_width/height */

Finally, run mpeg2encode:

 exec mpeg2encode /path/to/video.par_mpg /path/to/video.mpg
 puts "mpeg2encode done."

AM With TclMagick I have been able to automate even more of this (I do not need to define a parameters file like the above - thereby I lose some control - but still).

Here is the script I used:

 #
 # Small script to make an MPEG file
 #
 package require TclMagick

 #
 # Get a list of suitable GIF files - all of them living in the current directory
 #
 set gifs [glob anim*.gif]

 #
 # Now create the magic wand that will do all the processing
 #
 set mw [magick create wand]

 foreach f $gifs {
    $mw read $f
    $mw next 
 }

 #
 # Make an MPEG file
 #
 $mw WriteImages justatest.mpg yes

 #
 # Or if this suits you better, an animated GIF  
 #
 #$mw WriteImages justatest.gif yes

See tk3play which is (or was...) a MPEG audio player.