Updating video properties |
Required introduction
In the previous section, we read one of the video properties, path. A video has several basic properties like Title, Link, Description, FilePath, and Rating. You can view these properties at the top of the video details window.
When writing video properties, you must be very careful as the changes you make are permanent. Always have a backup of your video catalog, especially when creating new scripts that alter the catalog.
When a video is indexed, it can read metadata for titles ( video indexer tab in preferences). If you read a video title from metadata and that metadata is nonsense, it is possible to reset this to be the video file's name with the help of a script. Let's continue to build on our previous example and make a script that takes the video path, gets the filename from that path, and sets that as the video's title.
First, we need a bit of string manipulation to get the filename from the path.
LastIndexOf searches a string from the end for a given character and returns the 0 based index of the found character (or -1 if it was not present in the string).
int filename_start = path.LastIndexOf("\\");
We search from the end to find the \ character. In C#, the \ is special and denotes the beginning of a special character sequence. That is why we need to write two \. Once to start a special sequence and second to get the character.
Next, we grab a substring of the path starting at one character after the value of the filename_start variable.
string filename = path.Substring( filename_start+1 );
To set the title of a video we use this function on the scripting interface
void SetVideoProperty( long video_file_id, string property_name, string value )
The first argument is the id of the video, the second is a name string of the property we want to change, and the third is the property's new value.
catalog.SetVideoProperty( VideoID, "Title", filename );
If we combine it with the previous example, the whole program should now look like this:
using System.Runtime; using System.Collections.Generic; using VideoCataloger; using VideoCataloger.RemoteCatalogService; class Script { static public async System.Threading.Tasks.Task Run ( IScripting scripting, string arguments ) { ISelection selection = scripting.GetSelection(); List<long> selected_videos = selection.GetSelectedVideos(); IVideoCatalogService catalog = scripting.GetVideoCatalogService(); IUtilities utils = scripting.GetUtilities(); foreach (long VideoID in selected_videos) { VideoFileEntry entry = catalog.GetVideoFileEntry( VideoID ); string path = utils.ConvertToLocalPath(entry.FilePath); int filename_start = path.LastIndexOf("\\"); string filename = path.Substring( filename_start+1 ); catalog.SetVideoProperty( VideoID, "Title", filename ); scripting.GetConsole().WriteLine( filename ); } } }
Running the script will update the title of the selected video to be the filename. But you will not see the change unless you search or deselect and then select the video again. Since we access the catalog directly, the user interface is not aware of the update.
We can tell the user interface to refresh. We get the user interface API from GetGUI() in IScripting and use the Refresh function like this,
scripting.GetGUI().Refresh("");
Put the refresh call at the end of the script and the ui will be refreshed to show the updated paths.