Originally created by: simon@…
I've modified the code to just add a number to the end of the filename if the timestamp is the same as in the previous file:
(It's my first try with python, but it's working...)
def find_mods(src_dir, cb = None):
""" command line interface for modcopy
@param src_dir: source directory to search for MOD/MOI
@param dest_dir: destination directory for MPG files
"""
assert(os.path.exists(src_dir))
LOG.info("Search %s for MOD files...", src_dir)
mods = [ ]
counter = 0
old_mod_mtime = ""
for root, dirs, files in os.walk(src_dir):
for mod_fn in files:
fn, ext = os.path.splitext(mod_fn)
if cb:
cb.on_status("Scanning %s/%s..." % (root, mod_fn))
if ext.upper() != ".MOD":
continue
fnbase = os.path.join(root, fn)
moi_fp = "".join((fnbase, ".MOI"))
mod_fp = os.path.join(root, mod_fn)
if os.path.exists(moi_fp):
md = get_moi_details(moi_fp)
# NOTE: assume there is only one recording taken within one second
# MOI timestamps have 1 minute resolution, MOD filetime 1 second
# use MOD filetime to avoid duplicate filenames
unix_mod_mtime = os.stat(mod_fp).st_mtime
mod_mtime = datetime.datetime.fromtimestamp(unix_mod_mtime)
mod_size_bytes = os.stat(mod_fp).st_size
#check whether time stamp was the same as previous and change the filename in this case
new_mod_mtime = mod_mtime.strftime("%Y%m%d-%H%M%S")
if old_mod_mtime != new_mod_mtime:
old_mod_mtime = new_mod_mtime
counter = 0
else:
counter += 1
mpg_fn = "MOV-%s_%d.MPG" % (new_mod_mtime, counter)
mod = (root, mod_fn, mpg_fn, md, mod_mtime, mod_size_bytes)
if md["video_format"] > 0:
dar = "16:9"
else:
dar = "4:3"
mods.append(mod)
LOG.info("Found %-20s [%s] %4s %4i MB",
mod_fn,
mod_mtime.isoformat(" "),
dar,
mod_size_bytes / 1E+6 )
if cb:
cb.on_mod(mod)
else:
LOG.warn("no associated MOI file [%s] for [%s]", moi_fp, mod_fn)
return mods