Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Friday, October 28, 2011

Change file timestamp

A crude python script to change modify/access file timestamps, after the new NAS would incorrectly change them to current time. Note: only changes modify and access time. File create time cannot be changed by standard python calls. From quick online research, it is suggested lower level, OS dependent, privileged system calls or crudely changing the system clock is required to change creation times.
import sys, os, time

sys.exit()  # hardcode the changed timestamp below

stime1 = "08/24/2010  06:24 PM" # everyday food video
stime2 = "06/29/2010  04:31 PM" # neely video
ts = time.strptime(stime2, "%m/%d/%Y  %I:%M %p")

print ts
print time.mktime(ts)
setTimeVal = time.mktime(ts)

print sys.argv[0]
print len(sys.argv)
if len(sys.argv) < 2:
 print "usage ", sys.argv[0], " <filename>"
 sys.exit(-1)

filename = sys.argv[1]

ts = os.stat(filename)
print ts

os.utime(filename, (setTimeVal,setTimeVal))

Friday, June 24, 2011

srt subtitle offset delay adjust fix utility

A quick and dirty basic python utility to adjust delay offset all the timestamps in a .srt (subtitle) file by a fixed number of seconds or fractions of a second. I know there are other .srt subtitle utilities, with far more functionality, and I have used them in the past. Since I only needed one basic function for what I want to do, I thought throwing this together would be just as fast as searching for it on the web or transferring .srt and video files to the other computer where I have subtitle editing utilities installed, .
# srt-offset.py
# offset srt timestamp
import sys
import re

if len(sys.argv) < 3:
 print("usage: " + sys.argv[0] + " <srt file> <timestamp adjust (sec)>")
 exit()

filename = sys.argv[1]
sec = int(float(sys.argv[2]))
msec = int((float(sys.argv[2])-sec)*1000)

#print(sec, msec)

f = open(filename, "r")

lines = f.readlines()

y = (2,3,4)

# format: 00:56:44,621 --> 00:56:49,998
r = re.compile(r'(..):(..):(..),(...) --> (..):(..):(..),(...)')
for l in lines:
 if l.find(' --> ') > 0 :
  m = r.search(l)
  g = list(m.groups())
  for i in range(0,len(g)):
    g[i] = int(g[i])
  g[3]  = msec
  g[2]  = sec
  if g[3]%1000 != g[3]:
   g[2]  = (g[3] - g[3]%1000)/1000
   g[3] = g[3]%1000
  if g[2]%60 != g[2]:
   g[1]  = (g[2] - g[2]%60)/60
   g[2] = g[2]%60
  if g[1]%60 != g[1]:
   g[0]  = (g[1] - g[1]%60)/60
   g[1] = g[1]%60
  g[7]  = msec
  g[6]  = sec
  if g[7]%1000 != g[7]:
   g[6]  = (g[7] - g[7]%1000)/1000
   g[7] = g[7]%1000
  if g[6]%60 != g[6]:
   g[5]  = (g[6] - g[6]%60)/60
   g[6] = g[6]%60
  if g[5]%60 != g[5]:
   g[4]  = (g[5] - g[5]%60)/60
   g[5] = g[5]%60

#  print('*', end='')
  l = "%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d\n" % tuple(g)

 print(l, end='')

Saturday, May 28, 2011

basic rectangle intersection algorithm

I was sifting through LWUIT sourcecode, and came upon a function for determining if 2 rectangular areas intersect at any region. I stared for a painfully long time at the simple task and simple algorithm to figure out why it worked. It works by comparing the lower right and top left point of each rectangle. For there to be an intersection by rectangles r1 and r2, the top left of r1 is inside the quadrant described by the lower right of r2, and vice versa. If either of these is not true, it means r1 lies entirely outside a quadrant described by one of the corners of r2, which means the rectangles do not intersect ie, they are disjoint.
# intersect.py
# check if rectangles intersect
# rectangles defined by
# (tx,ty, tw=width, th=height)
# (x,y, width, height)

def intersects(tx, ty, tw, th, x, y, width, height):
        rw = width
        rh = height

        if (rw <= 0 or rh <= 0 or tw <= 0 or th <= 0): 
            return false
        
        rx = x
        ry = y
        rw += rx
        rh += ry
        tw += tx
        th += ty

        # The first sub-test checks if rw/rh/tw/th overflowed, meaning wrapped around to value < rx/ry/tw/ry
        return ((rw < rx or rw > tx) and
                (rh < ry or rh > ty) and
                (tw < tx or tw > rx) and
                (th < ty or th > ry))

def main():
 print(intersects(1,1,4,5,3,3,1,1))
 print(intersects(3,3,1,1,1,1,4,5))

main()


Friday, May 20, 2011

python script to replace html Entities (&; codes)

This script converts html files in place, replacing html character entities with standard characters. I'm using this to clean up html entities unrecognized by the Anyview j2me javame midlet ebook reader for LG dlite gd570 phone.
# htmlcodefix.py (dlc 5/2011)
# replace html entity codes .  writes new html file in place.
import sys
import os
import re

trans = { "&ldquo;" : "\"",
 "&nbsp;" : " ",
 "&rdquo;" : "\"",
 "&rsquo;" : "'", 
 "&mdash;" : "--", 
 }

def main():
 if len(sys.argv) != 2:
  print("Usage: " + sys.argv[0] + " {html file to convert in-place}")
  exit();
 filename = sys.argv[1]
 if(not filename.endswith(("htm", "html"))):
  print(filename + " not html!")
  exit();
 if(not os.path.exists(filename)): 
  print(filename + " not found!")
  exit();

 print("converting ... " + filename)
 with open(filename, 'r') as f:
  read_data = f.read()
 
 for (srch, repl) in trans.items():
  print (srch, repl)
  read_data = read_data.replace(srch, repl)

 print(read_data)

 with open(filename, mode='w') as f:
  f.write(read_data)



if __name__ == '__main__':
 main()