Extending the video search

The basic idea I show you here is how to write a script in C# that determines if a video matches your search criteria or not and put the matching videos in a custom Bin.

You will learn by a simple step by step example how to write a script that goes through all videos in you catalog and puts all short videos(the critera we have as an example) in a bin.

After you have this skill you can expand your skill and continue to experiment.

About the native video search

Fast video cataloger has a very powerful, fast and simple search engine. The text below show how easy you can extend it even more with advanced custom video search in C#.

Our simple user interface make searching in Fast video cataloger straight forward for everyone. However, we do not provide a SQL interface for search so there are searches you can simply not express given our user interface. With the scripting interface you can do basically anything.

AI For example… What if you want to search with facial recognition or object detection or the next big thing in AI and video?

You an do all this and more by leveraging the scripting capabilities in Fast video cataloger

step by step breakdown on the Video search script in c#

Run(…) in the code below, is the entry function where the execution of the script starts.

    static public void Run(IScripting scripting, string argument)
    {
        FilterToBin instance = new FilterToBin(scripting);
        instance.SetBinTarget("short videos");
        instance.Filter();
        scripting.GetGUI().Refresh("");
    }

I have encapsulated all functionality in a class called “FilterToBin” that has two functions.

SetBinTarget() let you set the name of the Bin that will be used for search results. Please note that this Bin will be cleared every time you run the search.

Filter() does the actual filtering of videos.

    private void Filter()
    {
        var catalog = m_Scripting.GetVideoCatalogService();

        VideoQuery query = new VideoQuery();
        VideoFileEntry[] videos = catalog.SearchVideos(query);

        long bin_id = CreateBin();

        foreach (VideoFileEntry entry in videos)
        {
            if (IsVideoPassingFilter(entry))
                catalog.AddVideoToBin(entry.ID, bin_id);
        }
    }

The Filter() function first does a search to get all videos. In this example we pass in en empty VideoQuery object. That will give us all videos in the catalog. You can of course leverage the search here and first do a high level search to get a subset of your videos and then apply a more advanced search on that subset.

We then call CreateBin() to ensure we have a bin to put our results in. If we look closer in the FilterToBin class the CreateBin member function has some logic to create a Bin if its not already created and clear it from videos if it already exists.

Finally we iterate over all videos and call the IsVideoPassingFilter() function on each video. Videos that pass the test are put in the result Bin.

    private bool IsVideoPassingFilter( VideoFileEntry entry )
    {
        if ( entry.LengthSeconds < 60)
            return true;
        return false;
    }

If you want to change the filter criteria this is the only function you need to edit. In this example we only check if the video length property is smaller than 60s.

To run this script you copy paste it or type it (or load it) into the script window and then click the Run button.

Go the Bin window and select the "short videos" to see the result (if the bin is not there go into the edit bin to refresh the window, this is needed on older versions of Fast video cataloger).

Edit button in bin window

Edit button in bin window

Full source code in C#

using VideoCataloger;
using VideoCataloger.RemoteCatalogService;

public class FilterToBin
{
    IScripting m_Scripting;
    string m_BinLabel;

    FilterToBin(IScripting scripting)
    {
        m_Scripting = scripting;
    }

    private void SetBinTarget(string bin_label)
    {
        m_BinLabel = bin_label;
    }

    private void Filter()
    {
        var catalog = m_Scripting.GetVideoCatalogService();

        VideoQuery query = new VideoQuery();
        VideoFileEntry[] videos = catalog.SearchVideos(query);

        long bin_id = CreateBin();

        foreach (VideoFileEntry entry in videos)
        {
            if (IsVideoPassingFilter(entry))
                catalog.AddVideoToBin(entry.ID, bin_id);
        }
    }

    private bool IsVideoPassingFilter( VideoFileEntry entry )
    {
        if ( entry.LengthSeconds < 60)
            return true;
        return false;
    }

    private long CreateBin()
    {
        var catalog = m_Scripting.GetVideoCatalogService();
        Bin[] all_bins = catalog.GetAllBins();
        foreach (Bin bin in all_bins)
        {
            if (bin.Label == m_BinLabel)
            {
                VideoFileEntry[] videos = catalog.GetVideosInBin(bin.BinID);
                foreach (VideoFileEntry entry in videos)
                {
                    catalog.RemoveVideoFromBin( bin.BinID, entry.ID );
                }

                return bin.BinID;
            }
        }

        Bin target_bin = catalog.CreateBin(m_BinLabel, -1, 0xffffff);
        return target_bin.BinID;
    }

    static public void Run(IScripting scripting, string argument)
    {
        FilterToBin instance = new FilterToBin(scripting);
        instance.SetBinTarget("short videos");
        instance.Filter();
        scripting.GetGUI().Refresh("");
    }
}