Say you have a file that isn’t supported by Comskip or ShowAnalyzer that you’d like to automatically scan for commercials. There are a couple of reasons for this; either the codec isn’t supported or the container isn't (or both). Fortunately the approach to solving both issues is the same. In this post I’m going to discuss the simplest way to get this working using DVRMSToolbox (DTB). At the end we will use DTB to automatically create a full fidelity version of the file that can be used for commercial detection and also as a source for transcoding using other tools if the codec or container is not natively supported in that tool.
- DTB 188.8.131.52+
- AVIVO (optional if creating a custom stub graph, required if using the one included)
- GraphEdit (optional, officially part of the platform SDK but can be obtained from various sources)
- Proppage.dll (optional, must regsvr32 to be useful. officially part of the platform SDK but can be obtained from various sources)
The MPEG conversion graph:
There are two actions in DTB that allow a completely arbitrary stub graph to be used for file conversion, ConvertWithGraph and ConvertWithExplicitGraph. Because ConvertWithGraph relies on Intelligent Connect (IC) to build the graph it is generally much easier to work with, since the goal here is to create a full scale .mpg I’m going to use it instead of ConvertWithExplictGraph. ConvertWithExplictGraph will use IC, but only to connect source pins with the initial video and audio pins on the filter specified in the XML definition that must be provided. The XML document defines the connections from that point on, so filters that would not normally wire up via IC can be used to do things like scale, rotate, or whatever the filter does. An example profile (“Convert a file for commercial scanning”) that uses ConvertWithExplictGraph is included in DTB 184.108.40.206+ if anyone is interested in using the action and needs a starting point.
In the included stub graph (makemempg.grf) I’ve added all of the filters necessary to convert a file from whatever format it is in to a mpg.
Note that if it’s a container issue (i.e. the streams are mpeg video and mpeg audio or AC-3) two of the filters are unnecessary in the stub (ATI MPEG Video Encoder and ATI MPEG Audio Encoder), but will be bypassed when the source file is rendered in most cases. It’s easy to see if they will for the source file by opening the stub in GraphEdit and selecting “Render Media File” to add the source to the graph. The image below is an example of what happens when a DVR-MS file is rendered.
If it’s an unsupported codec, and the demuxer/decoder filters that have higher merit (merit is a number that DirectShow uses to prioritize what filter gets added when using IC to connect filters) are the ones that produce the best results on your machine the stub can be used as-is. If however, an undesirable filter has higher merit some modification is necessary to convert the file. Again, we can find out by opening the stub in GraphEdit and using “Render Media File” to find out what filters will join automatically, and if the file will convert properly.
This image shows the results on one of my PCs when I render a HD PVR TS recording.
And this shows the results when rendering a WMV file.
In both cases some additional configuration is necessary to make the conversion work. Using the WMV graph as an example; we want the A/V output pins on the WMV source filter (which is also a splitter) to connect to the MPEG A/V encode filters and not directly to the muxer. So some additional filters need to be added to the stub to make it work. Fortunately we can use IC to help us determine which decoder we should add by deleting the connection between the A/V output pins and the muxer and connecting them to the respective input pin on the MPEG encoder filters.
Now we know that we should start with the WMAudio Decoder DMO and WMVideo Decoder DMO. Close GraphEdit and reopen the stub, select Graph -> Insert Filters from the menu bar.
Browse the dialog that opens to find and add the filters (either double click on the filter or select it and click “Insert Filter”).
Make sure to save the stub before continuing, then “Render Media File” to test that everything wires up correctly. It should look something like the image below.
Test conversion by clicking the “Play” button. If everything works, you’re ready for the next step.
So what do you do when it doesn’t work? Here’s where it gets more complicated. In simple cases like the one with the TS file, the wrong filters get added when merit is used, you just need to find the right filters and add them to the stub like we did with the WM Decoder DMO filters. Usually, professional filters from companies like Cyberlink or ArcSoft work the best. For HD PVR TS files, I’ve had the best luck with the ArcSoft ones that come with TMT (that’s what “Convert a file for commercial scanning” uses) but your mileage will vary. For more complex cases where even though the correct filter is part of the stub, but it still won’t wire up you’ll have to use ConvertWithExplictGraph.
Creating a profile:
Now that the stub is working it’s time to create a DTB profile that will run the graph and scan the mpeg it creates for commercials.
Open up the DTB Settings tool, select (but don’t double click) “Find Commercials in DVR-MS”, and click “New Profile From” to create a new profile. Give your profile a name and a description. If the file that you want to use this for was not created by Media Center remove “Get Video Information” from the profile.
Click Add, and select the ConvertWithGraph, ChangeContextValue, and optionally FileOperation if you want to delete the mpeg when you’re done (the included profile does not have FileOperation).
As the actions are added, the configuration dialog will display. Configure ConvertWithGraph to use the stub created earlier, note that if the grf file is in the DTB install dir you do not have to enter the entire path (if it’s not, just double click the text box and browse to where it is). You can check "Run as fast as possible" but keep in mind that some source filters don't like this option (the graph will crash), so if you have a problem make sure you uncheck this first. To enhance compatibility I'm going to leave it unchecked, but feel free to play with it.
Configure ChangeContextValue to use "InputFile" to change the value of "OutputFile" using “([^\\]+)\.\w+\z” as the FindRegex and “$1.mpg” as the ReplaceRegex (without quotes). This will change the file extension of the input file from whatever it was to mpg, ensuring that if you want to use the edl or xml file that will be created for the source file it will be named correctly.
The actions will not be in the correct order when they are added, so you will need to move them to the correct location using the “Up” and “Down” buttons. Place ChangeContextValue directly under “Get Video Information” (if it is there; "Throttle DVRMStoMPEG" if it is not) and ConvertWithGraph directly below ChangeContextValue.
Double click on “Find Commercials” and change the ContextFIle to “OutputFile”.
Save the profile, you should see it in the list of profiles now.
Watching for files:
Before clicking “Save” in the settings tool, go to the File Watcher tab and add the file extension that you want to watch for to the “Watched Ext” field using “,.ext” syntax (don’t enter the quotes). Then click "Save" to save the settings and exit the tool.
Create a Processing Condition:
Almost done, but now you have a decision to make. If this process should run on every file that the File Watcher is looking for you can modify the “Every File” Processing Condition (PC) to use the new profile. If it’s more selective than that, a custom PC will be required to handle the process.
For completeness lets create a custom PC. Open the PC Editor and select “New Condition”, add a description, select the profile that was just created from the drop down, and click “Add” to create the metadata condition that will match the target file extension. Make sure to substitute the real extension that you want to match for in the place of ".ext".
The end result should look like something like this.
Save the condition, and change the priority to the correct location (somewhere above “Every File”). Then click “Save” to exit the editor.
Make sure it works:
No one likes to test, but we should probably check our work. Open up the DTB GUI tool and select an example of the target file type as the input, put anything that you want to into the output field (it’s going to change anyway), deselect “Use Selected Profile” (this tests PC evaluation), and click “Run”.
Assuming that it all goes smoothly you’re all set.
If you'd like to use the files I created for this, they are available for download. Extracting them to the DTB install directory should put them in the right place, but please be aware that if "C:\Users\Public\DvrmsToolbox\DTBTemp" does not exist on your computer the graph will fail to load.