合并百度影音的离线数据(withpython2.3)

四种格式的解析:

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):
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\s+):(?p\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)

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

Posted in 未分类

发表评论