MAWT
offers parts of the video functionality of FFmpeg at the Tcl scripting level. This is accomplished by wrapping parts of the FFmpeg library with the help of SWIG.
Important links:
Get the last error message issued by MAWT.
See also: GetFfmpegVersion, GetVersion, SetDebugMode
proc ::mawt::GetErrorMessage {} { # Get the last error message issued by MAWT. # # See also: GetFfmpegVersion GetVersion SetDebugMode return [mawtlib::MawtGetErrorMessage] }
Get FFMPEG library version string.
Returns the version of the wrapped FFMPEG library as a string. The version is returned as Major.Minor.Patch
.
See also: GetVersion, SetDebugMode, GetErrorMessage
proc ::mawt::GetFfmpegVersion {} { # Get FFMPEG library version string. # # Returns the version of the wrapped FFMPEG library as a string. # The version is returned as `Major.Minor.Patch`. # # See also: GetVersion SetDebugMode GetErrorMessage return [mawtlib::MawtGetFfmpegVersion] }
Get MAWT library version string.
Returns the version of the MAWT library as a string. The version is returned as Major.Minor.Patch
.
See also: GetFfmpegVersion, SetDebugMode, GetErrorMessage
proc ::mawt::GetVersion {} { # Get MAWT library version string. # # Returns the version of the MAWT library as a string. # The version is returned as `Major.Minor.Patch`. # # See also: GetFfmpegVersion SetDebugMode GetErrorMessage return [mawtlib::MawtGetVersion] }
Enable or disable the debug mode of the MAWT library.
onOff | Boolean value to enable or disable the debug mode. |
See also: GetFfmpegVersion, GetVersion, GetErrorMessage
proc ::mawt::SetDebugMode {onOff} { # Enable or disable the debug mode of the MAWT library. # # onOff - Boolean value to enable or disable the debug mode. # # See also: GetFfmpegVersion GetVersion GetErrorMessage mawtlib::MawtSetDebugMode [expr {bool($onOff)}] }
Export video frames into a series of image files.
movieFile | Movie file name. |
imgFilePattern | Pattern for image output files. Must contain a printf like integer format specifier. |
args | Options described below. |
-end <int> | End frame. Default: Last video frame. |
-start <int> | Start frame. Default: First video frame. |
See also: VideoImport
proc ::mawt::VideoExport {movieFile imgFilePattern args} { # Export video frames into a series of image files. # # movieFile - Movie file name. # imgFilePattern - Pattern for image output files. # Must contain a printf like integer format specifier. # args - Options described below. # # -start <int> - Start frame. Default: First video frame. # -end <int> - End frame. Default: Last video frame. # # See also: VideoImport set startFrame -1 set endFrame -1 foreach { key value } $args { switch -exact $key { "-start" { set startFrame [expr {int($value)}] } "-end" { set endFrame [expr {int($value)}] } } } set videoObj [mawt Video new $movieFile "r"] set numBytes [$videoObj Start] set tx [$videoObj GetWidth] set ty [$videoObj GetHeight] set vectorObj [mawt Vector new $numBytes] image create photo VideoFrame -width $tx -height $ty set numFrames [$videoObj GetNumFrames] if { $startFrame < 0 } { set startFrame 0 } if { $endFrame < 0 || $endFrame > $numFrames } { set endFrame [expr {$numFrames - 1}] } set fmtStr "PNG" set optStr "" set numExportedImgs 0 for { set i $startFrame } { $i <= $endFrame } { incr i } { $videoObj Lock $videoObj GetNextImage [$vectorObj Get] 0 $videoObj Unlock $vectorObj ToPhoto VideoFrame $tx $ty set outFileName [format $imgFilePattern $i] set retVal [catch { VideoFrame write $outFileName -format "$fmtStr $optStr" } errMsg] if { $retVal != 0 } { puts "Error saving image:\n$errMsg" return false } incr numExportedImgs } $vectorObj destroy $videoObj destroy return $numExportedImgs }
Generate a video by importing a series of image files.
imgFilePattern | Pattern for image output files. Must contain a printf like integer format specifier. |
movieFile | Movie file name. |
args | Options described below. |
-bitrate <float> | Bitrate in MBit per second. Default: 4.0. |
-end <int> | End frame. Default: Last video frame. |
-flip <bool> | Flip images. Default: false. |
-fps <int> | Frames per second. Default: 25. |
-start <int> | Start frame. Default: First video frame. |
-verbose <bool> | Print verbose messages. Default: false. |
See also: VideoExport
proc ::mawt::VideoImport {imgFilePattern movieFile args} { # Generate a video by importing a series of image files. # # imgFilePattern - Pattern for image output files. # Must contain a printf like integer format specifier. # movieFile - Movie file name. # args - Options described below. # # -start <int> - Start frame. Default: First video frame. # -end <int> - End frame. Default: Last video frame. # -fps <int> - Frames per second. Default: 25. # -bitrate <float> - Bitrate in MBit per second. Default: 4.0. # -flip <bool> - Flip images. Default: false. # -verbose <bool> - Print verbose messages. Default: false. # # See also: VideoExport set startFrame -1 set endFrame -1 set flip 0 set verbose 0 foreach { key value } $args { switch -exact $key { "-start" { set startFrame [expr {int($value)}] } "-end" { set endFrame [expr {int($value)}] } "-flip" { set flip [expr {bool($value)}] } "-verbose" { set verbose [expr {bool($value)}] } } } if { $startFrame < 0 } { set startFrame 0 } if { $endFrame < 0 } { for { set i $startFrame } { true } { incr i } { set inFileName [format $imgFilePattern $i] if { ! [file exists $inFileName] } { set endFrame [expr {$i -1}] break } } } if { $verbose } { puts "Input files: $imgFilePattern" puts "Output file: $movieFile" puts "Start : $startFrame" puts "End : $endFrame" } set numImportedImgs 0 for { set i $startFrame } { $i <= $endFrame } { incr i } { set inFileName [format $imgFilePattern $i] set phImg [image create photo -file $inFileName] set w [image width $phImg] set h [image height $phImg] if { ! [info exists videoObj] } { set videoObj [mawt Video new $movieFile "w" -width $w -height $h {*}$args] set vectorObj [mawt Vector new [expr {$w * $h * 3}]] } $vectorObj FromPhoto $phImg 3 1 0 image delete $phImg $videoObj WriteNextImage [$vectorObj Get] $w $h $flip incr numImportedImgs } if { $numImportedImgs > 0 } { $videoObj Close $vectorObj destroy $videoObj destroy } return $numImportedImgs }
constructor | Constructor for the class. |
destructor | Destructor for the class. |
FromByteArray | Create a Vector from a binary string. |
FromPhoto | Not documented. |
Get | Not documented. |
ToByteArray | Copy a Vector into a binary string. |
ToPhoto | Not documented. |
numBytes | Not documented. |
method constructor {numBytes} { if { $numBytes > 0 } { set mVec [mawtlib::new_UByte $numBytes] } }
method destructor {} { mawtlib::delete_UByte $mVec }
Create a Vector from a binary string.
str | Binary string. |
Create a new Vector from given binary string $str
.
See also: ToByteArray
method FromByteArray {str} { # Create a Vector from a binary string. # # str - Binary string. # # Create a new Vector from given binary string $str. # # See also: ToByteArray set numBytes [string length $str] if { $numBytes == 0 } { error "String has zero length" } set mVec [mawtlib::new_UByte $numBytes] mawtlib::VectorFromByteArray $str $mVec $numBytes 0 0 }
phImg | Not documented. |
numChans | Not documented. |
scale | Not documented. |
offset | Not documented. |
method FromPhoto {phImg numChans scale offset} { return [mawtlib::VectorFromPhoto $phImg $mVec $numChans $scale $offset] }
method Get {} { return $mVec }
Copy a Vector into a binary string.
numBytes | int |
srcOff | int Optional, default 0 . |
destOff | int Optional, default 0 . |
Copy $numBytes
elements into a Tcl binary string and return that string. $srcOff
and $destOff
may be used optionally to specify an offset into the source and the destination.
See also: FromByteArray
method ToByteArray {numBytes {srcOff 0} {destOff 0}} { # Copy a Vector into a binary string. # # numBytes - int # srcOff - int # destOff - int # # Copy $numBytes elements into a Tcl binary string and return that string. # $srcOff and $destOff may be used optionally to specify # an offset into the source and the destination. # # See also: FromByteArray if { $numBytes == 0 } { error "Vector has zero length" } # First generate a binary string of appropriate size. set bytes [binary format a$numBytes 0] mawtlib::VectorToByteArray $mVec $bytes $numBytes $srcOff $destOff return $bytes }
phImg | Not documented. |
width | Not documented. |
height | Not documented. |
numChans | Not documented. Optional, default 3 . |
linesAreBottomUp | Not documented. Optional, default 0 . |
method ToPhoto {phImg width height {numChans 3} {linesAreBottomUp 0}} { mawtlib::VectorToPhoto $mVec $phImg $width $height $numChans $linesAreBottomUp }
constructor | Constructor for the class. |
destructor | Destructor for the class. |
Close | Close a video stream. |
Create | Create a new empty video file. |
GetDuration | Get video duration information as a dict. |
GetDurationAsString | Get video duration information as a string. |
GetFramerate | Not documented. |
GetHeight | Get the height of a video stream. |
GetImage | Get an image from a video stream. |
GetNextImage | Get next image from a video stream. |
GetNumFrames | Not documented. |
GetWidth | Get the width of a video stream. |
IsOpen | Not documented. |
Lock | Lock a video stream. |
Open | Open a video file. |
Start | Start acquisition of a video stream. |
Stop | Stop acquisition of a video stream. |
Unlock | Unlock a video stream. |
WriteNextImage | Write next image to a video stream. |
WriteNextPhotoImage | Write next photo image to a video stream. |
fileName | Not documented. |
mode | Not documented. |
args | Optional arguments. |
method constructor {fileName mode args} { set mVideoFile "" set mVideoId "NULL" set mNumBytes -1 set width 256 set height 256 set fps 25 set bitrate 4.0 foreach { key value } $args { switch -exact $key { "-width" { set width [expr {int($value)}] } "-height" { set height [expr {int($value)}] } "-fps" { set fps [expr {int($value)}] } "-bitrate" { set bitrate [expr {double($value)}] } } } if { $mode eq "r" } { my Open $fileName } else { my Create $fileName $width $height $fps $bitrate } set mMode $mode }
method destructor {} { if { [my IsOpen] } { my Close } }
Close a video stream.
See also: Open
method Close {} { # Close a video stream. # # See also: Open if { $mMode eq "r" } { mawtlib::MawtVideoClose $mVideoId } else { if {! [mawtlib::MawtVideoFinish $mVideoId] } { throw [list Video Close] [mawtlib::MawtGetErrorMessage] } } set mVideoId "NULL" set mVideoFile "" }
Create a new empty video file.
fileName | Create the video stream in specified file. |
width | Width of the video stream. |
height | Height of the video stream. |
fps | Framerate of the video streams (frames per second). |
bitrate | Not documented. |
birate | Bit rate of the video ostream in MBits/s. |
Throws an error, if the file could not be openend.
See also: Close, WriteNextImage, WriteNextPhotoImage
method Create {fileName width height fps bitrate} { # Create a new empty video file. # # fileName - Create the video stream in specified file. # width - Width of the video stream. # height - Height of the video stream. # fps - Framerate of the video streams (frames per second). # birate - Bit rate of the video ostream in MBits/s. # # Throws an error, if the file could not be openend. # # See also: Close WriteNextImage WriteNextPhotoImage if { [my IsOpen] } { my Close } set mVideoId [mawtlib::MawtVideoCreate $fileName $width $height $fps $bitrate] if { ! [my IsOpen] } { throw [list Video Create] [mawtlib::MawtGetErrorMessage] } set mVideoFile $fileName }
Get video duration information as a dict.
Get the duration of current video stream. The duration is returned as a dictionary with the keys: minutes
, seconds
, frames
.
See also: GetDurationAsString
method GetDuration {} { # Get video duration information as a dict. # # Get the duration of current video stream. # The duration is returned as a dictionary with the # keys: `minutes`, `seconds`, `frames`. # # See also: GetDurationAsString set numFrames [mawtlib::MawtVideoGetNumFrames $mVideoId] set frameRate [expr int([mawtlib::MawtVideoGetFramerate $mVideoId] + 0.5)] set secs [expr $numFrames / $frameRate] dict set durDict minutes [expr {$secs / 60}] dict set durDict seconds [expr {$secs - [dict get $durDict minutes] * 60}] dict set durDict frames [expr {$numFrames % $frameRate}] return $durDict }
Get video duration information as a string.
Get the duration of current video stream. The duration is returned as a string in the following format: minutes:seconds.frames
.
See also: GetDuration
method GetDurationAsString {} { # Get video duration information as a string. # # Get the duration of current video stream. # The duration is returned as a string in the following # format: `minutes:seconds.frames`. # # See also: GetDuration set durDict [my GetDuration] return [format "%02d:%02d.%02d" [dict get $durDict minutes] [dict get $durDict seconds] [dict get $durDict frames] ] }
method GetFramerate {} { return [mawtlib::MawtVideoGetFramerate $mVideoId] }
Get the height of a video stream.
Returns the height of a video stream in pixels.
method GetHeight {} { # Get the height of a video stream. # # Returns the height of a video stream in pixels. # # See also: Open GetWidth return [mawtlib::MawtVideoGetHeight $mVideoId] }
Get an image from a video stream.
img | A Vector of appropriate size to store the image data. |
flip | Boolean value to specify image flipping. |
num | Image number as integer value starting at zero. |
Get image $num
from current video stream. The image will be stored in variable $img
. $img
must point to a buffer of appropriate size. If $flip
is set to true, the image is returned in bottom-up scanline order as needed by OpenGL. If $flip
is set to false, the image is returned in top-down scanline order as needed by the img::raw extension.
Throws an error, if the image could not be retrieved from the video stream.
See also: Open, GetNextImage, GetWidth, GetHeight
method GetImage {img flip num} { # Get an image from a video stream. # # img - A Vector of appropriate size to store the image data. # flip - Boolean value to specify image flipping. # num - Image number as integer value starting at zero. # # Get image $num from current video stream. # The image will be stored in variable $img. # $img must point to a buffer of appropriate size. # If $flip is set to true, the image is returned in # bottom-up scanline order as needed by OpenGL. # If $flip is set to false, the image is returned in # top-down scanline order as needed by the img::raw extension. # # Throws an error, if the image could not be retrieved # from the video stream. # # See also: Open GetNextImage GetWidth GetHeight if { ! [mawtlib::MawtVideoGetImage $mVideoId $img [expr {bool($flip)}] $num] } { throw [list Video GetImage] [mawtlib::MawtGetErrorMessage] } }
Get next image from a video stream.
img | A Vector of appropriate size to store the image data. |
flip | Boolean value to specify image flipping. |
Get the next image from current video stream. The image will be stored in variable $img
. $img
must point to a buffer of appropriate size. If $flip
is set to true, the image is returned in bottom-up scanline order as needed by OpenGL. If $flip
is set to false, the image is returned in top-down scanline order as needed by the img::raw extension.
Throws an error, if the image could not be retrieved from the video stream.
See also: Open, Close, GetImage, GetWidth, GetHeight
method GetNextImage {img flip} { # Get next image from a video stream. # # img - A Vector of appropriate size to store the image data. # flip - Boolean value to specify image flipping. # # Get the next image from current video stream. # The image will be stored in variable $img. # $img must point to a buffer of appropriate size. # If $flip is set to true, the image is returned in # bottom-up scanline order as needed by OpenGL. # If $flip is set to false, the image is returned in # top-down scanline order as needed by the img::raw extension. # # Throws an error, if the image could not be retrieved # from the video stream. # # See also: Open Close GetImage GetWidth GetHeight if { ! [mawtlib::MawtVideoGetNextImage $mVideoId $img [expr {bool($flip)}]] } { # TODO: Check why this throws an error at the last frame. # throw [list Video GetNextImage] [mawtlib::MawtGetErrorMessage] } }
method GetNumFrames {} { return [mawtlib::MawtVideoGetNumFrames $mVideoId] }
Get the width of a video stream.
Returns the width of a video stream in pixels.
method GetWidth {} { # Get the width of a video stream. # # Returns the width of a video stream in pixels. # # See also: Open GetHeight return [mawtlib::MawtVideoGetWidth $mVideoId] }
method IsOpen {} { if { $mVideoId ne "NULL" } { return true } else { return false } }
Lock a video stream.
method Lock {} { # Lock a video stream. # # See also: Open Unlock mawtlib::MawtVideoLock $mVideoId }
Open a video file.
fileName | Open the video stream stored in specified file. |
If a video stream is already open, it is closed before opening the new video stream.
Throws an error, if the file could not be openend.
method Open {fileName} { # Open a video file. # # fileName - Open the video stream stored in specified file. # # If a video stream is already open, it is closed before opening # the new video stream. # # Throws an error, if the file could not be openend. # # See also: Close Start Stop if { [my IsOpen] } { my Close } set mVideoId [mawtlib::MawtVideoOpen $fileName] if { ! [my IsOpen] } { throw [list Video Open] [mawtlib::MawtGetErrorMessage] } set mVideoFile $fileName }
Start acquisition of a video stream.
width | integer Optional, default -1 . |
height | integer Optional, default -1 . |
Start acquisition of current video stream. If $width
and $height
are specified and greater than zero, stretch the video to that size. Otherwise the video is played in its original size.
Returns the number of bytes needed for a video frame. If the video could not be started, an error is thrown.
method Start {{width -1} {height -1}} { # Start acquisition of a video stream. # # width - integer # height - integer # # Start acquisition of current video stream. # If $width and $height are specified and greater than # zero, stretch the video to that size. # Otherwise the video is played in its original size. # # Returns the number of bytes needed for a video frame. # If the video could not be started, an error is thrown. # # See also: Open Stop set mNumBytes [mawtlib::MawtVideoStart $mVideoId $width $height] if { $mNumBytes < 0 } { throw [list Video Start] [mawtlib::MawtGetErrorMessage] } return $mNumBytes }
Stop acquisition of a video stream.
method Stop {} { # Stop acquisition of a video stream. # # See also: Start Open Close mawtlib::MawtVideoStop $mVideoId }
Unlock a video stream.
method Unlock {} { # Unlock a video stream. # # See also: Open Lock mawtlib::MawtVideoUnlock $mVideoId }
Write next image to a video stream.
img | A Vector containing the image data. |
width | Width of the image. |
height | Height of the image. |
flip | Boolean value to specify image flipping. |
Write the next image to the current video stream.
The image data must be available in variable $img
. $img
must point to a buffer of size width by height. If $flip
is set to true, the image is returned in bottom-up scanline order as needed by OpenGL. If $flip
is set to false, the image is returned in top-down scanline order as needed by the img::raw extension.
Throws an error, if the image could not be written to the video stream.
See also: WriteNextPhotoImage, Create, Close, GetImage, GetWidth, GetHeight
method WriteNextImage {img width height flip} { # Write next image to a video stream. # # img - A Vector containing the image data. # width - Width of the image. # height - Height of the image. # flip - Boolean value to specify image flipping. # # Write the next image to the current video stream. # # The image data must be available in variable $img. # $img must point to a buffer of size width by height. # If $flip is set to true, the image is returned in # bottom-up scanline order as needed by OpenGL. # If $flip is set to false, the image is returned in # top-down scanline order as needed by the img::raw extension. # # Throws an error, if the image could not be written # to the video stream. # # See also: WriteNextPhotoImage Create Close GetImage GetWidth GetHeight if { ! [mawtlib::MawtVideoWriteNextImage $mVideoId $img $width $height [expr {bool($flip)}]] } { # TODO: Check why this throws an error at the last frame. # throw [list Video WriteNextImage] [mawtlib::MawtGetErrorMessage] } }
Write next photo image to a video stream.
phImg | A Tk photo image. |
flip | Boolean value to specify image flipping. |
The image data must be available in photo image $phImg
.
If $flip
is set to true, the image is returned in bottom-up scanline order as needed by OpenGL. If $flip
is set to false, the image is returned in top-down scanline order as needed by the img::raw extension.
Throws an error, if the image could not be written to the video stream.
See also: WriteNextImage, Create, Close, GetImage, GetWidth, GetHeight
method WriteNextPhotoImage {phImg flip} { # Write next photo image to a video stream. # # phImg - A Tk photo image. # flip - Boolean value to specify image flipping. # # The image data must be available in photo image $phImg. # # If $flip is set to true, the image is returned in # bottom-up scanline order as needed by OpenGL. # If $flip is set to false, the image is returned in # top-down scanline order as needed by the img::raw extension. # # Throws an error, if the image could not be written # to the video stream. # # See also: WriteNextImage Create Close GetImage GetWidth GetHeight set w [image width $phImg] set h [image height $phImg] set vectorObj [mawt Vector new [expr {$w * $h * 3}]] $vectorObj FromPhoto $phImg 3 1 0 if { ! [mawtlib::MawtVideoWriteNextImage $mVideoId [$vectorObj Get] $w $h [expr {bool($flip)}]] } { # TODO: Check why this throws an error at the last frame. # throw [list Video WriteNextImage] [mawtlib::MawtGetErrorMessage] } $vectorObj destroy }