Jump to content

Automatically delete recordings older than x days


majstang

Recommended Posts

Found this nice script by mikeyes on the Autohotkey forum. Might be convenient for those who are fed up with deleting old recordings manually ;)

A RS process task executed daily will take care of this automatically. The script is very easy to configure. Autohotkey must be installed on your system in order to get it to work.

 

#NoEnv

OldConfigCleanup("2","E:\TV\Recording Service","*.ts") ; Recordings older than 2 days will be deleted.

OldConfigCleanup(Daystokeep,Pathtoconfigs,exttodelete)
{
 difdays = %Daystokeep%
 ;msgbox, difdays:%difdays%
 ;msgbox, current directory:%Pathtoconfigs%
 Source = %Pathtoconfigs%\%exttodelete%
 ;msgbox, source: %Source%
 Time := A_Now
 Loop, %Source%, 0, 0
 {
  TimeStamp := Time
  EnvSub, TimeStamp, %A_LoopFileTimeModified%, Days
  ;msgbox, timestamp for file %A_LoopFileLongPath% is %TimeStamp%
  If ( TimeStamp >= difdays )
  {
   ;MsgBox, file to delete: %A_LoopFileLongPath%   
   FileDelete, %A_LoopFileLongPath%
  }
  continue
 }
 ExitApp
}

Edited by majstang
Link to comment

Got a request on how to delete recordings in multiple folders, which is not supported in original script. Here's how it is done.

 

#NoEnv

OldConfigCleanup("2","E:\TV\Recording Service","*.ts") ; Recordings older than 2 days will be deleted from RS recording folder.
OldConfigCleanup("1","E:\TV\Timeshift","*.ts")         ; Timeshift files older than 1 day will be deleted from Timeshift folder.  

OldConfigCleanup(Daystokeep,Pathtoconfigs,exttodelete)
{
 difdays = %Daystokeep%
 ;msgbox, difdays:%difdays%
 ;msgbox, current directory:%Pathtoconfigs%
 Source = %Pathtoconfigs%\%exttodelete%
 ;msgbox, source: %Source%
 Time := A_Now
 Loop, %Source%, 0, 0
 {
  TimeStamp := Time
  EnvSub, TimeStamp, %A_LoopFileTimeModified%, Days
  ;msgbox, timestamp for file %A_LoopFileLongPath% is %TimeStamp%
  If ( TimeStamp >= difdays )
  {
   ;MsgBox, file to delete: %A_LoopFileLongPath%   
   FileDelete, %A_LoopFileLongPath%
  }
  continue
 }
 ;ExitApp
}

Edited by majstang
Link to comment
  • 1 month later...

Added a backup feature for the RS recordings database to this script. Unlike deleting recordings direct from RS webinterface, when deleting actual recordings/mediafiles with this script these entries remains in the recordings database, but if cleaning/refreshing db all entries will be lost. To avoid this and make the backup feature work, select both "Create EPG information file" and "Create Log file" in RS-->configuration-->Recorder. For every recording a .txt and .log file with same name as the recording will be created. The script copies the logfile to a subfolder of the RS Recordingfolder and moves the .txt to same folder. Script also changes the .log file extension to the .ts extension. In the script set your pathinformation according to your own conditions. When the script deletes your mediafiles/recordings it also attend to the entries in the recordings database will be safe from disappearing when cleaning/refreshing db.

 

The point for this feature is having a complete recordingsdatabase even if you are deleting the actual mediafiles/recordings, cuz a complete database is crucial to prevent recording of reruns in this new RS feature:

Add: Webinterface/Auto search: You can now select if the auto search should compare it's results with the recordings db (Title and/or Event). If a result is found in the db the auto search will create a DEACTIVATED timer.

For this to work, the recordings db has to be up to date and all recordings must be present.

More about it here: http://www.DVBViewer.tv/forum/topic/45835-disable-duplicate-timers/page__st__15

 

It's also nice to not have these .txt and .log files clutter the RS Recording folder. Since placing them in a subfolder to RS recordingfolder, RS recurse into that subfolder and picks up the recording entryinformation for the recordings database when refreshing db. So you now got a nifty little backup system, which hardly takes no harddrive space at all. Deletion of the recordings database entries can be done from RS webinterface-->Recordings tab (then all three files stored in the subfolder will be deleted).

 

As long as the actual mediafile in the RS recordingfolder is not deleted (in my case it won't be deleted until it is 4 days old), you will get duplicate entries of this very same recording in the recordings database. The duplicate entry will be removed when the mediafile gets deleted and database refreshed.

So, in my case I execute this script once a day (using a RS process task) and right after the script is done a RS internal task get executed, which refreshes the recordings database for me. In that way the backup is done and visible from RS webinterface.

 

ADDITION: If you for some reason want to edit the EPG information file .txt, for example adding the serial label to it, always use the webinterface editor for recordings. Don't do the same misstake i did trying to use Notepad for this task. For non english speakers this will screw up your .txt files bad. Notepad adds a BOM to the file and RS do not play nice with those (will give you garbage characters). Download and install Notepad++ and make it your editor of choice for all textfiles. When editing your .txt choose "Encoding" menu where you select "Encode in UTF-8 without BOM", hit save and RS shouldn't give you any problems...if you wanna do it this way.

 

#NoEnv

OldConfigCleanup("2","D:\TV\Inspelat","*.ts") ; Recordings older than 2 days will be deleted from Inspelat folder.
OldConfigCleanup("4","D:\TV\Recording Service","*.ts") ; Recordings older than 4 days will be deleted from Recording Service folder.
OldConfigCleanup("1","D:\TV\Timeshift","*.ts") ; Timeshifted Recordings older than 1 day will be deleted.
FileCopy, D:\TV\Recording Service\*.log, D:\TV\Recording Service\EPG Info-Log Files\ ; All RS log files will be copied to backupfolder 
FileMove, D:\TV\Recording Service\*.txt, D:\TV\Recording Service\EPG Info-Log Files\ ; All EPG information files will be moved to a subfolder
Filemove, D:\TV\Recording Service\*.log, D:\TV\Recording Service\EPG Info-Log Files\*.ts ; All RS Log files will be moved and renamed to .ts extension

OldConfigCleanup(Daystokeep,Pathtoconfigs,exttodelete)
{
 difdays = %Daystokeep%
 ;msgbox, difdays:%difdays%
 ;msgbox, current directory:%Pathtoconfigs%
 Source = %Pathtoconfigs%\%exttodelete%
 ;msgbox, source: %Source%
 Time := A_Now
 Loop, %Source%, 0, 0
 {
  TimeStamp := Time
  EnvSub, TimeStamp, %A_LoopFileTimeModified%, Days
  ;msgbox, timestamp for file %A_LoopFileLongPath% is %TimeStamp%
  If ( TimeStamp >= difdays )
  {
   ;MsgBox, file to delete: %A_LoopFileLongPath%   
   FileDelete, %A_LoopFileLongPath%
  }
  continue
 }
 ;ExitApp
}

Edited by majstang
Link to comment
  • 1 month later...

- Cleaned up the script a bit.

- Added a path and url section for configuration of the database backup feature.

- Added metadata/fileinfo copy (PropCopy).

- Added a stopper for backing up mediafiles when RS is recording them.

- Added a RS database refresh command when all recordings have been backed up.

- This script can be runned both as an RS after recording task and a RS Process task.

 

The only part for you to update (your paths and urls information) in order to get the script to work on your system is the

DELETE OLD RECORDINGS CONFIGURATION and PATH AND URL CONFIGURATION. Also make your choice if you want it running as an after

recording task or process task. Difference is only the recorded file gets backed up compared to all files up to 3 days old gets backed up. Instructions how to make the choice can be found in the script.

 

The metadata/fileinfo copy works as following. When we are filecopy/filemove .log, .txt, and make a "dummy".ts out of the .log, the metadata/fileinfo from the original.ts will automatically be copied over to our "dummy".ts. We need this metadata/fileinfo on the "dummy".ts, because without it the Prevent RS from recording reruns feature will not work. Read more about it in my two/three posts here:

http://www.DVBViewer.tv/forum/topic/45835-disable-duplicate-timers/page__view__findpost__p__341255

A tip is when installing RS check the "install the recording properties for Windows Explorer". Then you can see the recording properties when moving mouse cursor over file in the explorer. It's nice to be abled to see the result if the PropCopy failed or not.

 

The stopper for backup while RS is in recording was needed because if allowing backup while RS is recording we end up with uncomplete .log and .txt files in the EPGfiles/database backup subfolder to RS recording folder. That is bad from our point of view, because if the "dummy".ts with metadata/fileinfo from original.ts gets deleted for some reason the need arises to make a new "dummy".ts. When the original.ts is deleted and if the "dummy".ts also gets deleted there is currently no way of getting the metadata/fileinfo from a .txt to a new "dummy".ts, that works with the Prevent RS from recording reruns feature (the Autosearch "Event" feature fails to find the "dummy".ts metadata/fileinfo if copied from .txt). This database entry will become useless. Hopefully this feature will work in the future that will say...more info about that issue in the link above.

 

Adding of the code for the backup stopper allows in the same time full commanding of the RS Webinterface API. RS Database will be automatically refreshed when all files have been backed up. This means you can now remove the Refresh DB internal task timer and the RS Delete Old Recordings process timer and use this script as an after recording task :) It works like this:

U see if mediafile is still in recording the modification time equals the startime/creationtime. When recording stops modification time changes to a_now (time right now). When the after recording task executes this script, if timelimiter is set 10 seconds, only this recently recorded mediafile will be retrieved for filecopy/filemove and metadata processing. Other files like old recordings and even recordings that still are being recorded will be ruled out, cuz they are outside of time limit.

If for example having 10 tuners in the HTPC system and all 10 finishes thier recordings simultaneously, all 10 recordings will be picked up for database backup and metadata processing. If 5 recordings finishes simultaneously and 5 continues, only the 5 who finished will be picked up for processing.

 

Recording Service 1.9

Add: Autosearch: All recordings are now additionally added to the recordings history database. The entries will not be deleted, if you delete the actual recording. The check for previous recordings function of the auto recording timers now checks the search results against this database.

On every usage of the "Cleanup and Refresh Recording DB" task missing entries of existing recordings are added automatically to the history db.

Add: New task: Delete recording history.

The new feature Lars added in RS 1.9 is a huge improvement. It is still a good idea to use this script in order to keep a complete database when the actual recording is deleted. If an accidental "Clear Recording History" occurs you will be saved having kept a complete recording database. If such a occurance a simple "Refresh Recording DB" restores the recordings history database with our backup as basis.

 

Enjoy!

 

 

#NoEnv
;-----------------------------------------------------------------------------------------------------------------
;Activate codelines by removing the semicolon ";" infront of a line if you wanna use this feature
;Script can be used as an After Recording Task in RS or as a standalone script executed by a process timer. 
;Make your choice by activating one of the "EnvAdd" lines down below. Remember to inactivate the previous activated line. 
;Default is After Recording Task.  
;-----------------------------------------------------------------------------------------------------------------
;DELETE OLD RECORDINGS CONFIGURATION ;Fill in number of days to keep files and path to 'em
;-----------------------------------------------------------------------------------------------------------------
OldConfigCleanup("2","D:\TV\Inspelat","*.ts") ;Recordings older than 2 days will be deleted from Inspelat folder.
OldConfigCleanup("2","D:\TV\Seen Recordings","*.ts") ;Recordings older than 2 days will be deleted from Seen Recordings folder.
OldConfigCleanup("1","D:\TV\Timeshift","*.ts") ;Timeshifted Recordings older than 1 day will be deleted.
;-----------------------------------------------------------------------------------------------------------------
;PATH AND URL CONFIGURATION ;Fill in your path and URL information 
;-----------------------------------------------------------------------------------------------------------------
RS_recording_folder := "D:\TV\Recording Service" ;Fill in path to your RS recording folder
EPG_files_location := "D:\TV\Recording Service\EPG Info-Log Files" ;OBSERVE! Must be a subfolder to RS recording folder
PropCopy_location := "D:\AVI_VOB_BLU-RAY_AC3-tools\TV-tools\DVBViewer\PropCopy_1_2_2\PropCopy.exe" ;Fill in path to PropCopy
Recording_Service_API_URL := "http://username:password@127.0.0.1:8089" ;Fill in your user details, IP and port to the RS API
;-----------------------------------------------------------------------------------------------------------------
;CODE BODY
;-----------------------------------------------------------------------------------------------------------------
right_Now := a_now
timelimit := right_Now
EnvAdd, timelimit, -10, seconds ;Use this option in an After Recording Task. Only files less then 10 seconds old will be backed up.
;EnvAdd, timelimit, -3, days ;Use this option if executed outside of RS or from a RS process task. Files in RS recording folder up to 3 days old will be backed up.
FileList =
b_index=0
Loop, %RS_recording_folder%\*.log 
{ 
    if A_LoopFileTimeModified between %timelimit% and %right_Now% 
    {
       b_index++
	file%b_index% := A_LoopFileFullPath 
	;FileList = %FileList%%A_LoopFileTimeModified%`t%b_index%`t%A_LoopFileName%`n ;Enable this line to debug with msgbox Please hit
    }
} 
;msgbox Please hit ^c (while this is displayed) to copy to clipboard `n`n%FileList%
Loop %b_index% 
{ 
   SplitPath, file%a_index%, OutFileName, OutDir,, OutNameNoExt
   ;msgbox, %OutFileName%`n%OutDir%`n%OutNameNoExt% ;Enable this line to debug which files are coming into the loop 
   FileCopy, %OutDir%\%OutFileName%, %EPG_files_location%\ ;RS log file will be copied to backupfolder 
   FileMove, %OutDir%\%OutNameNoExt%.txt, %EPG_files_location%\ ;EPG information file will be moved to a subfolder
   Filemove, %OutDir%\%OutFileName%, %EPG_files_location%\%OutNameNoExt%.ts ;RS Log file will be moved and renamed to .ts extension
   RunWait, %PropCopy_location% "%RS_recording_folder%\%OutNameNoExt%.ts" "%EPG_files_location%\%OutNameNoExt%.ts" ;Metadata/Fileinfo copy    
}
UrlDownloadToVar(Recording_Service_API_URL . "/tasks.html?task=RefreshDB&aktion=tasks", OutputVar) ;Refresh Recording Service Database
;------------------------------------------------------------------------------------------------------------------
;FUNCTIONS
;------------------------------------------------------------------------------------------------------------------ 
OldConfigCleanup(Daystokeep,Pathtoconfigs,exttodelete)
{
 difdays = %Daystokeep%
 Source = %Pathtoconfigs%\%exttodelete%
 Time := A_Now
 Loop, %Source%, 0, 0
 {
  TimeStamp := Time
  EnvSub, TimeStamp, %A_LoopFileTimeModified%, Days
  If ( TimeStamp >= difdays )
  {
   ;MsgBox, file to delete: %A_LoopFileLongPath%   
   ;FileDelete, %A_LoopFileLongPath% ;Files gets deleted instantly and permanent. 
   FileRecycle, %A_LoopFileLongPath% ;Files gets moved to the Recycle Bin
  }
  continue
 }
 ;ExitApp
}
;-------------------------------------------------------------------------------------------------------------------
UrlDownloadToVar(URL, ByRef Result, UserAgent = "", Proxy = "", ProxyBypass = "") {
   pFix:=a_isunicode ? "W" : "A"
   hModule := DllCall("LoadLibrary", "Str", "wininet.dll") 
   AccessType := Proxy != "" ? 3 : 1
   io := DllCall("wininet\InternetOpen" . pFix
   , "Str", UserAgent ;lpszAgent 
   , "UInt", AccessType 
   , "Str", Proxy 
   , "Str", ProxyBypass 
   , "UInt", 0) ;dwFlags 
   iou := DllCall("wininet\InternetOpenUrl" . pFix
   , "UInt", io 
   , "Str", url 
   , "Str", "" ;lpszHeaders 
   , "UInt", 0 ;dwHeadersLength 
   , "UInt", 0x80000000 ;dwFlags: INTERNET_FLAG_RELOAD = 0x80000000 // retrieve the original item 
   , "UInt", 0) ;dwContext 
   If (ErrorLevel != 0 or iou = 0) { 
       DllCall("FreeLibrary", "UInt", hModule) 
       return 0 
   } 
   VarSetCapacity(buffer, 10240, 0)
   VarSetCapacity(BytesRead, 4, 0)
   Loop 
   { 
       irf := DllCall("wininet\InternetReadFile", "UInt", iou, "UInt", &buffer, "UInt", 10240, "UInt", &BytesRead) 
       VarSetCapacity(buffer, -1) ;to update the variable's internally-stored length
       BytesRead_ = 0 ; reset
       Loop, 4  ; Build the integer by adding up its bytes. (From ExtractInteger-function)
           BytesRead_ += *(&BytesRead + A_Index-1) << 8*(A_Index-1) ;Bytes read in this very DllCall
       If (irf = 1 and BytesRead_ = 0)
           break
       Else ; append the buffer's contents
       {
           a_isunicode ? buffer:=StrGet(&buffer, "CP0")
           Result .= SubStr(buffer, 1, BytesRead_ * (a_isunicode ? 2 : 1))
       }
   }
   DllCall("wininet\InternetCloseHandle",  "UInt", iou) 
   DllCall("wininet\InternetCloseHandle",  "UInt", io) 
   DllCall("FreeLibrary", "UInt", hModule)
}
;------------------------------------------------------------------------------------------------------------------

 

Edited by majstang
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...