// def SJQ4_METADATA = ["SJQ4_ID":"4622574", "SJQ4_TYPE":"MediaFile"] // remove the above line before using the scirpt in SJQv4. import org.apache.commons.io.FilenameUtils import org.apache.commons.io.FileUtils private class Settings { static public final boolean TEST_MODE = false } def mf = MediaFileAPI.GetMediaFileForID(SJQ4_METADATA["SJQ4_ID"].toInteger()) if(mf == null) { //make sure it's a valid file id println "Invalid media file id! [${SJQ4_METADATA['SJQ4_ID']}]" return 1 } // Let's set TASK_Status to 'running'. MediaFileAPI.SetMediaFileMetadata(mf, 'TASK_Status', 'CUT_RUNNING') def Prefix = FilenameUtils.getBaseName(MediaFileAPI.GetFileForSegment(mf, 0).getAbsolutePath()) def Origfile = new File(Prefix + ".ts") def VideoReDoFile = new File(Prefix + ".VPrj") def Origdir = ((MediaFileAPI.GetParentDirectory(mf).toString()) + "\\") def Origdir2 = (MediaFileAPI.GetParentDirectory(mf)) def base = FilenameUtils.getBaseName(Origfile.getName()) def CutExt = ".ts" def CutDir = "\\\\Sage-pc\\Mirror_Set\\Process_TV\\com_free\\" def FullInputFile = Origdir + VideoReDoFile def FullCutFile = CutDir + Origfile println FullCutFile // Cutting file using VideoRedo def i = 1 def file = new File(FullCutFile) while(file.exists()) file = new File(CutDir, "$base-${i++}$CutExt") println "New file is: $file" FullCutFile = file println "Cutting files that look like '${FullInputFile}' to '${FullCutFile}'." def command = ('cscript.exe //nologo "C:\\Program Files (x86)\\VideoReDoTVSuite4\\vp.vbs " "' + FullInputFile + '" "' + FullCutFile + '" "/p:H.264 Transport Stream" /q /e') println "full cut command will be '$command'" if(!Settings.TEST_MODE) { // Are we running test mode, if not do this def proc = command.execute() def initialSize = 4096 def outStream = new ByteArrayOutputStream(initialSize) def errStream = new ByteArrayOutputStream(initialSize) proc.consumeProcessOutput(outStream, errStream) proc.waitFor() println 'out:\n' + outStream println 'err:\n' + errStream } else { // if we are in test mode do this println "Would run '$command' if test mode were disabled!" } //relink ExternalID to new file if(!Settings.TEST_MODE) { // Are we running test mode, if not do this mf = MediaFileAPI.GetMediaFileForID(SJQ4_METADATA["SJQ4_ID"].toInteger()) def src = (FullCutFile.toString()) println "fullpathfile is=$src" def sid = ShowAPI.GetShowExternalID(mf) println "ExternalID is ${sid}" if(!Utility.IsFilePath(src)) { println "${src} does not exist!" return 1 } if(MediaFileAPI.GetMediaFileForFilePath(new File(src)) != null) { println "${src} is already a registered SageTV media file!" return 1 } def show = ShowAPI.GetShowForExternalID(sid) if(show == null) { println "ShowEID ${sid} is invalid!" return 1 } def mfadd = MediaFileAPI.AddMediaFile(new File(src), null) if(mfadd == null) { println "Failed to add media file!" return 1 } if(!MediaFileAPI.SetMediaFileShow(mfadd, show)) { println "Failed to link show metadata to media file!" return 1 } MediaFileAPI.SetMediaFileMetadata(mf, 'TASK_Status', 'CUT_DONE') def TASKStat = (MediaFileAPI.GetMediaFileMetadata(mf, 'TASK_Status')) println "TASK_Status set to '${TASKStat}'" MediaFileAPI.MoveTVFileOutOfLibrary(mfadd) println "Imported '${src}' and linked it to ShowEID '${sid}'!" } else { // if we are in test mode do this // println "Would have relinked the showID '${sid}' to the new file '${src}' if test mode were disabled!" } // Let's delete the original files since that was successful if(!Settings.TEST_MODE) { // Are we running test mode, if not do this Utility.DirectoryListing(Origdir2).each { if(FilenameUtils.wildcardMatchOnSystem(it.getName(), "${Prefix}.*")) { def fileName = new File(Origdir2, base + (it.getName().substring(it.getName().indexOf('.')))) try { fileName.delete() } catch(IOException e) { e.printStackTrace() } } } } else { // if we are in test mode do this println "Would delete ${Prefix}.* if test mode were disabled!" } return 0