异想天开

What's the true meaning of light, Could you tell me why

按时间顺序整理手机上的视频-python

日期:2015-07-31 20:01:57
  
最后更新日期:2015-08-04 15:34:22
需求:
按时间顺序整理手机微信上面的视频,安卓手机微信视频存放于一个文件夹,可以将该文件夹的视频不重复的导入到另外一个文件夹。该脚本的拷贝操作使用shutil库,能跨平台使用。目前微信版本(6.2.2)的命名规则是根据时间戳来命名。
依赖:
openssl命令,用来计算文件的md5
python环境

[code lang="cpp"]
# encoding: utf-8
#!/usr/bin/python

import os
import sys
#print sys.path
import string
import time
import platform
import shutil

reload(sys)
sys.setdefaultencoding('utf8')

com_one_day = {}
os_sys_ver = ""
'''
get the platform openssl name
'''
def OPENSSLCMD():
global os_sys_ver
cmd_default = "D:\Projects_MSDEV\openssl-1.0.2c\out32.dbg\openssl.exe"
os_sys_ver = platform.system()
if os_sys_ver == 'Linux':
return "openssl"
elif os_sys_ver == 'Windows':
try:
os.stat(cmd_default)
except OSError,e:
print e
return "openssl.exe"

return cmd_default

def get_os_ver():
global os_sys_ver
if os_sys_ver == "":
os_sys_ver = platform.system()
return os_sys_ver

'''
check the file if unique
'''
uniq_md5 = set()
def check_uniq_md5(file):
global uniq_md5
md5 = os.popen("%s md5 %s" % (OPENSSLCMD(),file)).read().split('=')[-1].split()[-1]
if not md5 in uniq_md5:
uniq_md5.add(md5)
return True
else:
return False

def walk_by(path,dest):
global com_one_day
if not path or not dest:
print "path and dest not null"
return

tree = os.walk(path)
for path,dirs,files in tree:
for file in files:
if path and file:
abs_path = path +"/" + file
if not check_uniq_md5(abs_path):
continue

st = os.stat(abs_path)
tt = st.st_mtime
month_path = dest + "/" + str(time.localtime(tt).tm_mon) + u"月"
try:
os.stat(month_path)
except OSError,e:
print e
os.mkdir(month_path)

tm_l = time.localtime(tt)
file_name = "%4d%02d%02d" % (tm_l.tm_year,tm_l.tm_mon,tm_l.tm_mday )
if com_one_day.has_key(file_name):
com_one_day[file_name] = com_one_day[file_name] + 1
else:
com_one_day[file_name] = 1

suffix = file.split(".")[-1]
new_path = month_path + "/%4d%02d%02d%02d.%s" % (tm_l.tm_year,tm_l.tm_mon,tm_l.tm_mday,com_one_day[file_name],suffix)
print abs_path,new_path
'''
abs_path and new_path convert to unicode
'''
if get_os_ver() == "Windows":
shutil.copyfile(unicode(abs_path,"gbk"),new_path)
elif get_os_ver() == "Linux":
shutil.copyfile(unicode(abs_path,"utf"),new_path)

if __name__=="__main__":
walk_by("./mp4","./mp4_new")
[/code]