合并百度影音的离线数据( with python 2.3)


Posted in Python onAugust 04, 2015

四种格式的解析:

filelist

slicelist

download.cfg

third_party_download.cfg

还是2个文件。替换之前版本即可。

初步测试正常,但时间轴不对。

代码如下:

# -*- coding: UTF-8 -*-
import os
import io
import codecs
import string
import shutil
import moviefmt
import traceback
def coroutine(func):
 def start(*args,**kwargs):
  g = func(*args,**kwargs)
  g.next()
  return g
 return start
@coroutine
def enum_movie_file(target): 
 selectedFolder = (yield)
 for fileitem in os.listdir(selectedFolder):    
  targetFO = os.path.join(selectedFolder,fileitem)  
  if(os.path.isfile(targetFO) == True):
   continue;  
  target.send(targetFO)  
@coroutine
def read_movie_file(funcDisp,target):
 while(True):
  filmFolder = (yield)
  for fileitem in os.listdir(filmFolder):  
   targetfile = os.path.join(filmFolder,fileitem)
   if(os.path.isfile(targetfile) == False):
    continue;   
   filebasename , fext = os.path.splitext(fileitem);    
   if(filebasename[0] == "."):
    continue   
   if(funcDisp.has_key(string.lower(fext))):
    pfn = funcDisp[string.lower(fext)];
    (targetFmt,targetObjects,filmFolderEx,filebasenameEx) = pfn(targetfile)
    if(targetFmt is None):
     continue;
    outputs = filmFolder,filebasename, (targetFmt,targetObjects,filmFolderEx,filebasenameEx);
    # outputs = filmFolder,filebasename, pfn(targetfile);
    target.send(outputs)
    break
def getWindowsText(orgText): 
 try:
  return orgText.encode('gbk') 
 except Exception as exc: 
  print("%s" % exc); 
  return orgText
@coroutine
def create_report(outputfile):
 objTar = codecs.open(outputfile,"w+")  
 while(1):
  filmFolder,filebasename,(extname,piece_list,filmFolderEx,filebasenameEx) = (yield)
  if(extname == None) :
   break;
  objTar.write('cd \"%s\"\r\n' % (filmFolderEx if filmFolderEx else filmFolder))  
  objTar.write('attrib -h *.*\r\n ')  
  if(len(piece_list) >1):
   objTar.write('copy /b ')  
  else:
   objTar.write('copy ')  
  if(len(piece_list) == 1):
   objTar.write('\"%s \"' %(getWindowsText(piece_list[0])))
  else:   
   bFirst = True
   for line in piece_list:
    if(bFirst == False):     
     objTar.write('+\"%s\"' %(getWindowsText(line))) 
    else:     
     objTar.write('\"%s\"' %(getWindowsText(line)))
     bFirst = False
  relpath = os.path.dirname(filmFolder)  
  currentPath = os.path.join(getWindowsText(relpath),getWindowsText(filebasenameEx if filebasenameEx else filebasename)) 
  objTar.write(' %s.%s\r\n' % (currentPath,extname))    
  objTar.write('cd %s\r\n' %(relpath))
 objTar.close()    
if __name__ == '__main__': 
 funcDisp = dict()
 funcDisp['.bdv_0000']= moviefmt.read_bdv_file;
 funcDisp['.rmvb_0000']= moviefmt.read_rmvb_file;
 funcDisp['.mkv_0000']= moviefmt.read_mkv_file;
 funcDisp['.bdv']= moviefmt.read_bdv_index;   
 funcDisp['.mp4_0000']= moviefmt.read_mp4_file;   
 funcDisp['']= moviefmt.read_mkv_2_file; 
 funcDisp['.filelist'] = moviefmt.read_filelist_index;
 funcDisp['.slicelist'] = moviefmt.read_slicelist_index;
 funcDisp['.cfg'] = moviefmt.read_cfg_index;
 try:
  rmf = enum_movie_file(read_movie_file(funcDisp,create_report('film.bat')))
  rmf.send(os.getcwd())
  rmf.close()
 except StopIteration:
  pass
 except Exception as exc: 
  print("%s" % exc); 
  info = traceback.format_exc()
  print(info)      
 print("done");  
# -*- coding: UTF-8 -*-
import os
import io
import sys
import string
import shutil
import codecs
import glob
import re
def read_bdv_index_V1(objFile):
 piece_list= list() 
 for line in objFile:
  if(line[0:4] != 'file'):
   continue;
  strings = string.split(line,'/')
  tarfile = strings[len(strings)-1]  
  tempText = string.strip(tarfile)
  piece_list.append(tempText)
 return "avi",piece_list,None,None
def read_bdv_index_V2(objFile):
 piece_list= list() 
 for line in objFile:  
  nPos = line.count('bdv')
  if( nPos <=0):
   continue;  
  tarfile = line[0:len(line)-2] 
  piece_list.append(tarfile) 
 return "mpeg",piece_list,None,None
def read_bdv_index_V3(objFile):
 piece_list= list()   
 oneFile= os.path.basename(objFile) 
 piece_list.append(oneFile)
 return "avi",piece_list,None,None
def read_bdv_index_V4(objFile):
 piece_list= list() 
 #skip #EXT-X-MEDIA-SEQUENCE
 objFile.readline();
 for line in objFile:
  if(line.startswith('#')==True):
   continue;  
  piece_list.append(line.replace('\r\n',''))
 return "avi",piece_list,None,None
def count_file_item(objFile,extText):
 cItem = 0;
 folderName = os.path.dirname(objFile)
 for fileitem in os.listdir(folderName): 
  filebasename , fext = os.path.splitext(fileitem); 
  if(fext == extText):
   cItem = cItem +1
 return cItem 
def read_bdv_index(filename):
 piece_list= list() 
 ext_type = None
 filmFolderEx = None
 filebasenameEx = None
 # total file count
 cItem = count_file_item(filename,".bdv")
 if(cItem == 1):
  ext_type,piece_list,filmFolderEx,filebasenameEx = read_bdv_index_V3(filename)
 else:
  objFile = codecs.open(filename,'r','utf-8') 
  topline = objFile.readline();
  bdv_ver = topline.replace("\r\n","")
  if(bdv_ver == '#EXTM3U'):
   v3Text = objFile.readline();
   if(v3Text.startswith('#EXT-X-TARGETDURATION') == False):
    ext_type,piece_list = read_bdv_index_V2(objFile)
   else:
    ext_type,piece_list = read_bdv_index_V4(objFile)
  else:
   ext_type,piece_list = read_bdv_index_V1(objFile)
  objFile.close()  
 return (ext_type ,piece_list,filmFolderEx,filebasenameEx)  
def read_bdv_file(filename):
 piece_list= list()
 piece_list.append('*.bdv_*')
 return ('avi',piece_list,None,None)
def read_rmvb_file(filename):
 piece_list= list()
 piece_list.append('*.rmvb_*')
 return ('rmvb',piece_list,None,None)
def read_mkv_file(filename):
 piece_list= list() 
 piece_list.append('*.mkv_*')
 return ('mkv',piece_list,None,None)
def read_mp4_file(filename):
 piece_list= list() 
 piece_list.append('*.mp4_*')
 return ('mp4',piece_list,None,None)
def read_mkv_2_file(filename):
 piece_list= list() 
 piece_list.append('video_*')
 return ('mkv',piece_list,None,None)
def read_filelist_index(filename):
 filmFolderEx = None
 filebasenameEx = None
 objFile = codecs.open(filename,'r','utf-8') 
 topline = objFile.readline();
 bdv_ver = topline.replace("\r\n","")
 if(bdv_ver == '#EXTM3U'):
  v3Text = objFile.readline();
  if(v3Text.startswith('#EXT-X-TARGETDURATION') == False):
   ext_type,piece_list,filmFolderEx,filebasenameEx = read_bdv_index_V2(objFile)
  else:
   ext_type,piece_list,filmFolderEx,filebasenameEx = read_bdv_index_V4(objFile)   
 else:
  ext_type,piece_list,filmFolderEx,filebasenameEx = read_bdv_index_V1(objFile)
 objFile.close()  
 return ("mp4" ,piece_list,filmFolderEx,filebasenameEx)    
def read_slicelist_index(filename):
 piece_list= list() 
 objFile = codecs.open(filename,'r','utf-8') 
 topline = objFile.readline();
 bdv_ver = topline.replace("\r\n","")
 if(bdv_ver == '#BD-SECTION'): 
  while(True):
   currentLine = objFile.readline();
   if(currentLine is None or len(currentLine) ==0):
    break
   piece_list.append(currentLine.replace("\r\n",""))
 objFile.close()  
 return ('mp4',piece_list,None,None)
def read_cfg_index(filename):
 piece_list= list() 
 simpleFileName = os.path.basename(filename)
 if(simpleFileName == 'download.cfg'):
  simpleFolderName = os.path.dirname(filename)
  chkLst =glob.glob(os.path.join(simpleFolderName, '*.filelist'));
  if(chkLst is not None and len(chkLst) >0):
   return (None,None,None,None)
  chkLst =glob.glob(os.path.join(simpleFolderName, '*.bdv'));
  piece_list.append(chkLst[0]);
  return ('avi',piece_list)
 if(simpleFileName != 'third_party_download.cfg'):
  raise Exception('unknown format file : %s' %(filename))
 reObj = re.compile('(?P<var_key>\S+):(?P<var_value>\S+)',re.IGNORECASE)
 lineDict = dict()
# [orgLines.append(orgLine.replace("\r\n","") ) for orgLine in codecs.open(filename,'r','utf-8')]
 for orgLine in codecs.open(filename,'r','utf-8'):
  orgLine = orgLine.replace("\r\n","")  
  results = reObj.findall(orgLine)
  if(len(results) > 0):
   lineDict[results[0][0]] =results[0][1]
 resID = lineDict["resource"] 
 vid = lineDict[ '%s_vid'%(resID)]
 targetName = lineDict["video_name"]
 simpleFolderName = os.path.dirname(filename)
 upLevelFolderName = os.path.dirname(simpleFolderName)
 targetFolder = os.path.join(upLevelFolderName,vid)
 targetFolder = os.path.join(targetFolder,vid)
 chkLst =glob.glob(os.path.join(targetFolder, '*.%s*' %(resID)));
 targetFile = chkLst[0] 
  #skip #EXT-X-MEDIA-SEQUENCE 
 for line in codecs.open(targetFile,'r','utf-8'):
  if(line.startswith('#')==True):
   continue;  
  line = os.path.basename(line.replace('\r\n',''))
  line = os.path.basename(line.replace('\r',''))
  line = os.path.basename(line.replace('\n',''))
  piece_list.append(line )
 return ('avi',piece_list,targetFolder,targetName)

以上就是本文的全部内容,希望大家喜欢。

Python 相关文章推荐
Python使用pip安装报错:is not a supported wheel on this platform的解决方法
Jan 23 Python
python3.5 email实现发送邮件功能
May 22 Python
python读取txt文件,去掉空格计算每行长度的方法
Dec 20 Python
pytorch 使用单个GPU与多个GPU进行训练与测试的方法
Aug 19 Python
python实现按关键字筛选日志文件
Dec 24 Python
TensorFlow自定义损失函数来预测商品销售量
Feb 05 Python
spyder 在控制台(console)执行python文件,debug python程序方式
Apr 20 Python
解决Opencv+Python cv2.imshow闪退问题
Apr 24 Python
python+requests接口自动化框架的实现
Aug 31 Python
python3 kubernetes api的使用示例
Jan 12 Python
Pandas加速代码之避免使用for循环
May 30 Python
详解Python函数print用法
Jun 18 Python
python利用datetime模块计算时间差
Aug 04 #Python
使用python加密自己的密码
Aug 04 #Python
Python实现字典的key和values的交换
Aug 04 #Python
python利用装饰器进行运算的实例分析
Aug 04 #Python
浅谈python多线程和队列管理shell程序
Aug 04 #Python
python中使用序列的方法
Aug 03 #Python
python实现备份目录的方法
Aug 03 #Python
You might like
Windows下的PHP5.0详解
2006/11/18 PHP
优化使用mysql存储session的php代码
2008/01/10 PHP
php中判断字符串是否全是中文或含有中文的实现代码
2011/09/16 PHP
ecshop 批量上传(加入自定义属性)
2012/03/20 PHP
php获取CSS文件中图片地址并下载到本地的方法
2014/12/02 PHP
PHP实现递归无限级分类
2015/10/22 PHP
php使用pdo连接sqlite3的配置示例
2016/05/27 PHP
JS获取整个页面文档的实现代码
2011/12/15 Javascript
jquery弹出框的用法示例(2)
2013/08/26 Javascript
extjs中form与grid交互数据(record)的方法
2013/08/29 Javascript
JavaScript Split()方法
2015/12/18 Javascript
Bootstrap每天必学之警告框插件
2016/04/26 Javascript
jQuery插件HighCharts绘制简单2D柱状图效果示例【附demo源码】
2017/03/21 jQuery
AngularJS中$http的交互问题
2017/03/29 Javascript
使用重写url机制实现验证码换一张功能
2017/08/01 Javascript
在vue中添加Echarts图表的基本使用教程
2017/11/22 Javascript
代码分析vue中如何配置less
2018/09/28 Javascript
详解Vue iview IE浏览器不兼容报错(Iview Bable polyfill)
2019/01/07 Javascript
python中的yield使用方法
2014/02/11 Python
python编写网页爬虫脚本并实现APScheduler调度
2014/07/28 Python
python3实现全角和半角字符转换的方法示例
2017/09/21 Python
python并发编程多进程 互斥锁原理解析
2019/08/20 Python
Python3.7实现验证码登录方式代码实例
2020/02/14 Python
django 多数据库及分库实现方式
2020/04/01 Python
HTML5实现的图片无限加载的瀑布流效果另带边框圆角阴影
2014/03/07 HTML / CSS
英国定做窗帘和纺织品面料一站式商店:Dekoria
2018/08/29 全球购物
捷克街头、运动和滑板一站式商店:BoardStar.cz
2019/10/06 全球购物
英国复古服装购物网站:Collectif
2019/10/30 全球购物
初中生个人学习的自我评价
2013/12/04 职场文书
运动会口号8字
2014/06/07 职场文书
淘宝活动总结范文
2014/06/26 职场文书
教师优秀党员事迹材料
2014/08/14 职场文书
党员十八大心得体会
2014/09/12 职场文书
委托收款证明
2015/06/23 职场文书
创业计划书之外语培训班
2019/11/02 职场文书
使用Python开发冰球小游戏
2022/04/30 Python