
"Roll Your Own GFA BASIC Program 7" by Troy H. Cheek on May 25, 2009
(This week's article will only make sense if you started at part 1.)
If Finis% > 0
MinX% = MinX% / Finis% - 1
MinY% = MinY% / Finis% - 1
MaxX% = Xres% - MaxX% / Finis%
MaxY% = Yres% - MaxY% / Finis%
MinX% = MinX% / 16
MinX% = MinX% * 16
MinY% = MinY% / 16
MinY% = MinY% * 16
MaxX% = MaxX% / 16
MaxX% = MaxX% * 16
MaxY% = MaxY% / 16
MaxY% = MaxY% * 16
MaxX% = Max(MinX%, MaxX%)
MaxY% = Max(MinY%, MaxY%)
If MaxX% < 33 Then MaxX% = 0
If MaxY% < 33 Then MaxY% = 0
If MaxX% + MaxY% > 0 Then Acf% = 1
Open EZMFilename$ for Append As # 2
Print # 2; Time$; " "; "Crop Top/Bottom:"; MaxY%; ",
Left/Right:"; MaxX%
Close # 2
Else
Open EZMFilename$ for Append As # 2
Print # 2; Time$; " "; LogFilename$; " not found or invalid."
Close # 2
Kill EZMFilename$
End
EndIf
The Finis flag was set earlier if the Comskip log file was processed properly. About the only times it wouldn't be processed properly is if there was something major wrong in the structure of the source video file or if the video file hadn't been finished recording yet or Comskip just hadn't finished processing it yet. If the Finis flag is not set, we log this, then delete the file we're logging things to. Yes, I know, that's wasted effort. But if I decide later to deal with the problem another way, I've already got the logging part done.
By removing the log file entirely, EZMedia will run again in the future when DirMon2 tells it to. Assuming that the problem was that Comskip just hadn't finished yet, Comskip should be finished by now, so the log file should be complete. If the problem was with the original MPG file, well, then eventually I'll notice that the same file keeps getting processed over and over. I'll probably just delete it and tell SageTV to record it again at some future point.
One of the great things about HandBrake is that it can crop videos as it converts them. Well, technically, it's impossible to crop a video without converting it, but you know what I mean. This is necessary because a widescreen movie formatted for a standard television set will have a bunch of black space added to the top and bottom to make it fit the screen (letterbox). A standard television show aired on a HD channel intended for widescreen televisions will have a bunch of black space added to the left and right (pillarbox). Sometimes, both get added (windowframe). It's especially annoying if I'm watching what was originally a widescreen video on a widescreen monitor but it's gone through a couple of rounds of boxing. It seems like I'm watching a postage stamp stuck in the middle of my monitor. As such, I try to determine where the actual video is and try to crop down to just that.
MinX is the number of black pixels on the left side of the video image during a given segment, as determined by Comskip. I try to pick only the segments that apply to the television program, not the commercials, and add them all togther. I then divide by the number of segments to get an average number of pixels. I then divide by and multiply by 16. This has the effect of rounding that average off to the nearest 16 pixels. MPEG-2 video cropping works best if you cut in increments of 16, or so the HandBrake warning dialog keeps telling me.
MinY (top) is handled the same way. MaxX (right) and MaxY (bottom) are handled similarly, except that Comskip counts the pixels from the upper left corner and Handbrake wants the number of pixels to throw away, so I have to subtract the average from the overall resolution.
Once all that's done, I combine MinX with MaxX and combine MinY with MaxY, in both cases taking the larger value. Sometimes the presense of a station logo or weather alert streamer causes Comskip to think that the actual video goes all the way to the bottom or one side, so this helps cut out some of that garbage.
If the number of pixels is 32 or smaller, then there's not really enough of a border to bother cutting out. HandBrake likes to cut it out anyway when left to its own devices (autocrop or something like that), so for very small values I just specify 0.
If it turns out that there is some cropping going on, I set the old Automatic Compression Flag to make sure that this file gets converted, just in case I'm only converting some of the files during this run.
If dur% > 75
Open EZMFilename$ for Append As # 2
Print # 2; Time$; " ";
Print # 2; "Movie identified (longer than 75 minutes)"
Close # 2
Acf% = 1
EndIf
As I mentioned before, long movies take up a lot of space, especially if they're aired in High Definition. Sometimes, a 50 year old black and white movie is aired in High Definition. I don't know what people are thinking. Anyway, if a movie is longer than an hour and a bit, I set the Automatic Compression Flag to make sure that it's converted.
If Acf% = 1
[stuff we'll discuss is part 8]
Else
Open EZMFilename$ for Append As # 2
Print # 2; Time$; " ";
Print # 2; "No reason to compress. Exiting."
Close # 2
EndIf
If the video file has been determined to be High Definition, if the AutoCompress plugin from SageTV has fired, if Comskip detected some boxing of the video, or if the file is more than an hour long, we compress it. Otherwise, if it's a short, nonboxed, standard defintion show, then we don't bother.
Next week, we'll finally get to the guts and actually compress some files!