Game Development Community

dev|Pro Game Development Curriculum

Simple Video Capture in Torque 3D

by Nathan Martin · 02/06/2011 (6:09 pm) · 15 comments


Introduction

Several days ago somebody came on IRC and was wondering about video capturing of Torque 3D for cutscenes. I dug around the source code because I recall seeing something about it while I've been trying to get OpenGL working on Windows (that'll be another resource later on). I Lo and behold found these two console functions:
void startVideoCapture(
  GuiCanvas  canvas,
  string  filename,
  string  encoder = "THEORA",
  float  framerate = 30.0f,
  Point2I  resolution = Point2I(0, 0)
)
void stopVideoCapture()

But there were no examples nor any support functions in the stock scripts that come with Torque 3D that gave any hint as to how to use these functions, so after taking some time to figure them out I programmed my own helper function that works similar to the existing game journal/demo recording support functions:
//-----------------------------------------------------------------------------
// Theora Video Capture (Live Encoding)
//-----------------------------------------------------------------------------

// settings
$Recordings::VideoCap::Codec		= "THEORA";		// video codec to encode with
$Recordings::VideoCap::Framerate	= 30;			// video capture frame rate
$Recordings::VideoCap::MaxFileNum	= 1000;			// maximum file number to try

// please leave alone
$Recordings::VideoCap::isCapping	= false;
$Recordings::VideoCap::CapFile		= "";
$Recordings::VideoCap::CapFileExt	= ".ogv";

// arguments:
//		action -- boolean
//				true	Start video capture recording.
//				false	Stop video capture.
//				NOTE:	True while already capturing then it's same as doing:
//							videoCapControl(false); videoCapControl(true);
//
function videoCapControl(%action)
{
	/// stop recording video
	if($Recordings::VideoCap::isCapping)
	{
		$Recordings::VideoCap::isCapping = false;
		stopVideoCapture();
		ChatHud.AddLine( "\c4Video Capture [\c2" @ $Recordings::VideoCap::CapFile @ "\cr] finished.");
	}

	// abort on stop action since its completed by now
	if(!%action)
		return;

	/// start recording video

	// locate a non-existent filename to use
	for(%i = 0; %i < $Recordings::VideoCap::MaxFileNum; %i++)
	{
		%num = %i;
		if(%num < 10)
			%num = "0" @ %num;
		if(%num < 100)
			%num = "0" @ %num;

		%file = $currentMod @ "/recordings/videoCap" @ %num @ "";
		if(!isfile(%file @ $Recordings::VideoCap::CapFileExt))
			break;
	}

	// failed to locate an available filename
	if(%i > $Recordings::VideoCap::MaxFileNum)
	{
		ChatHud.AddLine( "\c3 *** Video Capture Failed: No unused filenames!");
		return;
	}

	// remember video capture file
	$Recordings::VideoCap::CapFile		= %file @ $Recordings::VideoCap::CapFileExt;
	$Recordings::VideoCap::isCapping	= true;

	// now begin recording
	startVideoCapture(Canvas, %file, $Recordings::VideoCap::Codec, $Recordings::VideoCap::Framerate);
	ChatHud.AddLine( "\c4Started Video Capture [\c2" @ $Recordings::VideoCap::CapFile @ "\cr].");

	// done
}


Usage Example

You can use this in two ways either manually in console or via your own script routines, or by incorporating it to the core scripts and using hotkeys to invoke the function. To use the function as it is briefly described within the comments above the function declaration, here are the usage examples:
// start recording to game/tools/recordings/videoCap0000.ogv
videoCapControl(true);

// stop recording game/tools/recordings/videoCap0000.ogv
videoCapControl(false);


// start recording to game/tools/recordings/videoCap0001.ogv
videoCapControl(true);

// stop recording game/tools/recordings/videoCap0001.ogv
// and immediately start recording to game/tools/recordings/videoCap0002.ogv
videoCapControl(true);

// now stop recording game/tools/recordings/videoCap0002.ogv
videoCapControl(false);


Implementation

Now here are the instructions on implementing the script function into the core scripts in the Examples/FPS Example/game/ provided game example. For those of you who prefer a patch file instead of manually doing this modification may download the patch file from any of these two sites:
T3DVideoCapScript20110206.patch -- Site A
T3DVideoCapScript20110206.patch -- Site B

NOTE: I already know about the strange timestamps in the patch file. All I can say is I used the latest WinMerge version on Windows XP to generate the patch file, so must be a bug.

Now on to business:
Edit file core/scripts/client/recordings.cs
and insert the videoCapControl() function provided above at the end of the recordings.cs file.

Next, edit file core/scripts/gui/optionsDlg.cs and insert this snippet after line 472 just after the last $RemapCounter++; line:
$RemapName[$RemapCount] = "Start Video Capture";
$RemapCmd[$RemapCount] = "videoCapStart";
$RemapCount++;
$RemapName[$RemapCount] = "Stop Video Capture";
$RemapCmd[$RemapCount] = "videoCapStop";
$RemapCount++;

And last edit file scripts/client/default.bind.cs and insert this snippet around line 529 right after moveMap.bind( keyboard, F4, stopRecordingDemo );:
function videoCapStart( %val )
{
   if ( %val )
      videoCapControl(true);
}

function videoCapStop( %val )
{
   if ( %val )
      videoCapControl(false);
}

moveMap.bind( keyboard, "alt F3", videoCapStart );
moveMap.bind( keyboard, "alt F4", videoCapStop );


Results

After finishing the implementation now you can use Alt+F3 to start video capture and Alt+F4 to stop video capture. Now you can of course open the options dialog and remap these keys to whatever you want them to be. I chose these default keys since F3 and F4 start and stop game journal/demo recording respectively. I hope you all liked this resource as it has been a long time since my last resource posting. Enjoy! :)

About the author

By day I work as an embedded microcontroller programmer for mainly Blackfin MCU/DSPs, and by night, and free time too, I'm a hobbyist game developer.


#1
02/06/2011 (6:51 pm)
Glad we could prod you into posting this as a resource. I think the community will benefit from it greatly. Great work!
#2
02/06/2011 (9:32 pm)
Awesome. This will hopefully make my purchase of FRAPS unnecessary ;).
#3
02/07/2011 (1:53 am)
Lol with all the time using Torque I never knew it even had the basics of this!! Great resource and thanks for posting
#4
02/07/2011 (6:51 am)
Excellent!
#5
02/07/2011 (7:36 am)
Sweet. I bet even the devs don't know that this one is in there!
#6
02/07/2011 (9:24 am)
Very cool resource, I'm already seeing some very interesting things one could do with this.
#7
02/07/2011 (1:52 pm)
Actually we all know about it... TP just didn't do a good job of promoting it.

It was coded by Manoel Neto specifically for recording in game footage for promotional videos.

#8
02/07/2011 (3:31 pm)
Just a suggestion for the TS code, you didn't need to do the whole "$Recordings::VideoCap::MaxFileNum" + loop there.

Instead, you can do something like this:

//sample snippet...
   %last = 0;
   %search = $currentMod @ "/recordings/videoCap*"@$Recordings::VideoCap::CapFileExt@""; 
   for(%file = findFirstFile(%search); %file !$= ""; %file = findNextFile(%search)) {
      %number = strReplace(fileBase(%file), "videoCap", ""); // get the name of the script
      if(%number > %last) {
         %last = %number;
      }
   }
   %last++;
   %name = $currentMod @ "/recordings/videoCap"@%last@""@$Recordings::VideoCap::CapFileExt@"";

Just a little code snippet recommendation, still a nice find. :)
#9
02/10/2011 (7:11 am)
Awesome
#10
02/10/2011 (6:18 pm)
claps around the world.
#11
02/11/2011 (2:26 am)
Awesome! I really needed this, so thanks for contributing.
#12
02/28/2011 (6:23 am)
Working perfectly as its here.

startVideoCapture() also accepts an extra parameter, resolution; however after trying different resolutions with not compliant video results, I dropped it.

An extension of the video capture to also encode game audio would be a really good addition.
#13
04/23/2011 (5:33 pm)
Thanks it I needed this glad to be a part of forums!
#14
06/09/2011 (8:16 am)
Thanks for sharing this information.

Will try it in TGEA 1.8.2 and hopefully this will work :D
#15
11/18/2011 (9:54 am)
Hey, this is really cool stuff, I just came across it by accident, and it looks like something I'll be taking advantage of immediately.
#16
06/01/2012 (2:36 pm)
*Hoping this post isn't dead*
I looked into the source code and the video capture should be running on it's own thread. Anyone got a usefull performance out of this video capture?
For me it slows down the game alot, the playback is fine but the screen is really laggy while recording.