2012-06-15 21:34:07 +00:00
|
|
|
#!/mod/bin/jimsh
|
|
|
|
|
|
|
|
source /mod/webif/lib/setup
|
|
|
|
require lock system.class ts.class tdelete
|
|
|
|
|
2013-01-18 19:24:50 +00:00
|
|
|
set testing 0
|
|
|
|
|
2012-08-04 21:42:02 +00:00
|
|
|
proc dsc {} {
|
|
|
|
lassign [system diskspace] x x perc
|
|
|
|
if {$perc > 90} {
|
2013-01-27 21:23:16 +00:00
|
|
|
puts "Insufficient disk space ($perc%), terminating."
|
2012-08-04 21:42:02 +00:00
|
|
|
exit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dsc
|
|
|
|
|
2012-06-15 21:34:07 +00:00
|
|
|
if {![acquire_lock webif_auto]} {
|
|
|
|
puts "Cannot acquire exclusive lock, terminating."
|
|
|
|
exit
|
|
|
|
}
|
|
|
|
|
|
|
|
set tmp "/mod/tmp/webif_auto"
|
|
|
|
if {![file exists $tmp]} {
|
|
|
|
if {[catch {file mkdir $tmp} msg]} {
|
|
|
|
puts "Cannot create temporary directory - $tmp ($msg)"
|
|
|
|
exit
|
|
|
|
}
|
|
|
|
} elseif {![file isdirectory $tmp]} {
|
|
|
|
puts "Cannot create temporary directory - $tmp (file exists)"
|
|
|
|
exit
|
|
|
|
}
|
|
|
|
|
|
|
|
# Clean-up the temporary directory
|
2012-08-04 21:42:02 +00:00
|
|
|
foreach file [readdir -nocomplain $tmp] { tdelete "$tmp/$file" }
|
2012-06-15 21:34:07 +00:00
|
|
|
|
|
|
|
if {[system pkginst undelete]} {
|
|
|
|
set dustbin "[system dustbin]"
|
|
|
|
} else {
|
|
|
|
set dustbin ""
|
|
|
|
}
|
|
|
|
|
|
|
|
proc bindir {file binroot} {
|
|
|
|
set dir [file dirname $file]
|
|
|
|
regsub "^[system mediaroot]" $dir $binroot ndir
|
|
|
|
if {$dir eq $ndir} { set ndir $binroot }
|
|
|
|
system mkdir_p $ndir
|
|
|
|
return $ndir
|
|
|
|
}
|
|
|
|
|
|
|
|
proc dedup {dir} {
|
2013-01-18 19:24:50 +00:00
|
|
|
if {$::testing} {
|
|
|
|
puts "DEDUP: \[$dir]"
|
|
|
|
} else {
|
|
|
|
puts [exec /mod/webif/html/dedup/dedup -yes $dir]
|
|
|
|
exec /mod/webif/html/dedup/dedup -yes $dir
|
|
|
|
}
|
2012-06-15 21:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
proc do_shrink {ts} {
|
2012-08-07 22:22:30 +00:00
|
|
|
global tmp dustbin tsgroup
|
2012-06-15 21:34:07 +00:00
|
|
|
|
|
|
|
set file [file rootname [$ts get file]]
|
|
|
|
if {[catch {
|
2012-08-04 00:29:57 +00:00
|
|
|
set perc [exec /mod/bin/stripts -aq $file]
|
2012-06-15 21:34:07 +00:00
|
|
|
} msg]} {
|
|
|
|
puts " Error: $msg"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if {[string match {*%} $perc]} {
|
|
|
|
set perc [string range $perc 0 end-1]
|
|
|
|
} else {
|
|
|
|
set perc 0
|
|
|
|
}
|
|
|
|
|
|
|
|
if {$perc == 0} {
|
2012-06-24 20:45:07 +00:00
|
|
|
#puts " Already shrunk."
|
2012-06-15 21:34:07 +00:00
|
|
|
return
|
|
|
|
}
|
2012-06-24 20:45:07 +00:00
|
|
|
puts " SHRINK: $file"
|
2012-06-15 21:34:07 +00:00
|
|
|
puts " Estimate $perc% saving."
|
|
|
|
puts " Shrinking..."
|
|
|
|
if {[catch {
|
2013-01-08 22:43:43 +00:00
|
|
|
puts [exec nice -n 19 /mod/bin/stripts -q $file $tmp/shrunk]
|
2012-06-15 21:34:07 +00:00
|
|
|
} msg]} {
|
|
|
|
puts "Error during shrink: $msg"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
# The following steps are structured to minimise the risk of
|
|
|
|
# things being left in an inconsistent state if the system goes
|
|
|
|
# into standby. Renames within the same filesystem are very
|
|
|
|
# quick so the risk is small, but even so...
|
|
|
|
|
|
|
|
# Move the shrunken version back to the local directory.
|
|
|
|
foreach f [glob "$tmp/shrunk.*"] {
|
|
|
|
set ext [file extension $f]
|
|
|
|
file rename $f "${file}_shrunk${ext}"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Move the old recording to the bin if undelete is installed.
|
|
|
|
if {$dustbin ne ""} {
|
|
|
|
$ts move [bindir $file "$dustbin/webif_autoshrink"] 1 1
|
|
|
|
} else {
|
|
|
|
# Delete otherwise.
|
|
|
|
if {[$ts delete]} {
|
|
|
|
puts "Successfully deleted $file."
|
|
|
|
} else {
|
|
|
|
puts "Problem deleting $file, [$ts get error]"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Finally, rename the shrunken recording again.
|
2012-08-07 22:22:30 +00:00
|
|
|
foreach ext $tsgroup {
|
|
|
|
set f "${file}_shrunk.$ext"
|
|
|
|
if {[file exists $f]} {
|
|
|
|
file rename $f "${file}.$ext"
|
|
|
|
}
|
2012-06-15 21:34:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
proc do_decrypt {ts} {
|
|
|
|
global tmp dustbin
|
|
|
|
|
|
|
|
set file [$ts get file]
|
|
|
|
set rfile [file rootname $file]
|
|
|
|
set bfile [file tail $file]
|
|
|
|
|
|
|
|
if {![$ts flag "ODEncrypted"]} {
|
2012-06-24 20:45:07 +00:00
|
|
|
#puts " Already decrypted."
|
2012-06-15 21:34:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
lassign [$ts dlnaloc] url
|
|
|
|
if {$url eq ""} {
|
2012-06-24 20:45:07 +00:00
|
|
|
#puts " Not yet indexed."
|
2012-06-15 21:34:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-06-24 20:45:07 +00:00
|
|
|
puts " DECRYPT: $rfile"
|
2012-06-15 21:34:07 +00:00
|
|
|
puts " DLNA: $url"
|
|
|
|
exec wget -O "$tmp/$bfile" $url
|
|
|
|
|
2012-09-19 20:44:04 +00:00
|
|
|
if {[file size $file] != [file size "$tmp/$bfile"]} {
|
|
|
|
puts " File size mismatch."
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-06-15 21:34:07 +00:00
|
|
|
# Move the encrypted file out of the way.
|
|
|
|
file rename $file "$rfile.encrypted"
|
|
|
|
# Move the decrypted copy into place.
|
|
|
|
file rename "$tmp/$bfile" $file
|
|
|
|
# Patch the HMT - quickest way to get back to a playable file.
|
2012-08-04 21:42:02 +00:00
|
|
|
exec /mod/bin/hmt -encrypted "$rfile.hmt"
|
2012-06-15 21:34:07 +00:00
|
|
|
|
|
|
|
puts " Removing/binning old copy."
|
|
|
|
# Move the old recording to the bin if undelete is installed.
|
|
|
|
if {$dustbin ne ""} {
|
2012-11-05 21:57:29 +00:00
|
|
|
set bin [bindir $file "$dustbin/webif_autodecrypt"]
|
2012-06-15 21:34:07 +00:00
|
|
|
set tail [file tail $rfile]
|
|
|
|
file rename "$rfile.encrypted" "$bin/$tail.ts"
|
|
|
|
foreach ext {nts hmt thm} {
|
|
|
|
if {[file exists "$rfile.$ext"]} {
|
|
|
|
file copy $rfile.$ext "$bin/$tail.$ext"
|
|
|
|
if {$ext eq "hmt"} {
|
|
|
|
# Patch the binned HMT back
|
|
|
|
exec /mod/bin/hmt +encrypted \
|
2012-08-04 21:42:02 +00:00
|
|
|
"$bin/$tail.hmt"
|
2012-06-15 21:34:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2012-08-04 21:42:02 +00:00
|
|
|
tdelete "$rfile.encrypted"
|
2012-06-15 21:34:07 +00:00
|
|
|
}
|
|
|
|
puts " Done."
|
|
|
|
}
|
|
|
|
|
2013-01-08 22:43:43 +00:00
|
|
|
proc do_mpg {ts} {
|
|
|
|
global tmp tsgroup
|
|
|
|
|
|
|
|
set file [file rootname [$ts get file]]
|
|
|
|
|
|
|
|
if {[file exists $file.mpg]} {
|
|
|
|
# Already done.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if {[$ts flag "ODEncrypted"]} {
|
|
|
|
#puts " Not decrypted."
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if {[$ts get definition] eq "HD"} {
|
|
|
|
# Cannot extract a useful MP3 from a HD recording.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
puts " MPG: $file"
|
|
|
|
puts " Converting..."
|
|
|
|
if {[catch {
|
|
|
|
puts [exec nice -n 19 /mod/bin/ffmpeg -y -benchmark -v 0 \
|
|
|
|
-i $file.ts \
|
|
|
|
-map 0:0 -map 0:1 \
|
|
|
|
-vcodec copy -acodec copy $tmp/mpg.mpg]
|
|
|
|
} msg]} {
|
|
|
|
puts "Error during mpg extract: $msg"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
# Move the MPG into the local directory
|
|
|
|
file rename $tmp/mpg.mpg $file.mpg
|
|
|
|
}
|
|
|
|
|
2012-06-15 21:34:07 +00:00
|
|
|
proc entries {dir callback} {
|
|
|
|
foreach entry [readdir -nocomplain $dir] {
|
2012-08-04 21:42:02 +00:00
|
|
|
dsc
|
2012-06-15 21:34:07 +00:00
|
|
|
if {![string match {*.ts} $entry} continue
|
|
|
|
if {[catch {set ts [ts fetch "$dir/$entry"]}]} continue
|
|
|
|
if {$ts == 0} continue
|
|
|
|
if {[system inuse [file rootname "$dir/$entry"]]} {
|
|
|
|
puts "$entry - in use\n"
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
$callback $ts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
proc shrink {dir} {
|
|
|
|
puts "SHRINK: \[$dir]"
|
2013-01-18 19:24:50 +00:00
|
|
|
if {!$::testing} { entries $dir do_shrink }
|
2012-06-15 21:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
proc decrypt {dir} {
|
|
|
|
puts "DECRYPT: \[$dir]"
|
2013-01-18 19:24:50 +00:00
|
|
|
if {!$::testing} { entries $dir do_decrypt }
|
2012-06-15 21:34:07 +00:00
|
|
|
}
|
|
|
|
|
2013-01-08 22:43:43 +00:00
|
|
|
proc mpg {dir} {
|
|
|
|
puts "MPG: \[$dir]"
|
2013-01-18 19:24:50 +00:00
|
|
|
if {!$::testing} { entries $dir do_mpg }
|
2013-01-08 22:43:43 +00:00
|
|
|
}
|
|
|
|
|
2013-01-18 19:24:50 +00:00
|
|
|
proc scan {dir attr {force 0}} {{indent 0}} {
|
2013-01-27 21:23:16 +00:00
|
|
|
global dustbin
|
|
|
|
|
2012-06-15 21:34:07 +00:00
|
|
|
incr indent 2
|
|
|
|
|
2013-01-18 19:24:50 +00:00
|
|
|
if {$::testing} { puts "[string repeat " " $indent]\[$dir]" }
|
2012-06-15 21:34:07 +00:00
|
|
|
|
2013-01-27 21:23:16 +00:00
|
|
|
#if {[string match {\[*} $dir]} continue
|
|
|
|
if {$dir eq $::dustbin} {
|
|
|
|
puts "Dustbin, skipping."
|
|
|
|
return
|
|
|
|
}
|
2012-06-16 21:57:24 +00:00
|
|
|
|
2012-08-04 21:42:02 +00:00
|
|
|
dsc
|
2013-01-18 19:24:50 +00:00
|
|
|
|
|
|
|
# Recursion
|
|
|
|
if {[file exists "$dir/.auto${attr}r"]} {
|
|
|
|
if {$::testing} { puts "[string repeat " " $indent] (R)" }
|
|
|
|
set force 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if {$force || [file exists "$dir/.auto$attr"]} { $attr $dir }
|
2012-06-15 21:34:07 +00:00
|
|
|
|
|
|
|
foreach entry [readdir -nocomplain $dir] {
|
|
|
|
if {[file isdirectory "$dir/$entry"]} {
|
2013-01-18 19:24:50 +00:00
|
|
|
scan "$dir/$entry" $attr $force
|
2012-06-15 21:34:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
incr indent -2
|
|
|
|
}
|
|
|
|
|
|
|
|
set root [system mediaroot]
|
|
|
|
|
|
|
|
if {[llength $argv] > 0} {
|
|
|
|
foreach arg $argv { scan $root $arg }
|
|
|
|
} else {
|
2013-01-08 22:43:43 +00:00
|
|
|
foreach arg {dedup decrypt shrink mpg} {
|
2012-06-15 21:34:07 +00:00
|
|
|
scan $root $arg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
release_lock webif_auto
|
|
|
|
|