合并百度影音的离线数据 with python 2.2 bdv格式的更新

百度影音的bdv格式又有变化。

此次存在2种bdv格式。

格式1:每个文件夹内就一个bdv文件,文件合并后改名avi即可。

格式2:每个文件夹内一个bdv文件作为索引,其他附加guid的文件作为数据。

例如:

#EXTM3U
#EXT-X-TARGETDURATION:30
#EXT-X-MEDIA-SEQUENCE:0
#EXTINF:10,

 

所以python脚本有改。

该脚本修正了单个文件在批处理过程名称。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# -*- coding: UTF-8 -*-
 
import os
import io
import codecs
import string
import shutil
import moviefmt
 
 
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(funcDisp.has_key(string.lower(fext))):
                pfn = funcDisp[string.lower(fext)];
                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) = (yield)
        if(extname == None) :
            break;
 
 
        objTar.write(‘cd \"%s\"\r\n‘ % filmFolder)     
         
        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
                 
 
        objTar.write(‘  ..\\"%s\".%s\r\n‘ %(filebasename,extname))             
 
        objTar.write(‘cd ..\r\n‘)
 
    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;   
    try:
        rmf = enum_movie_file(read_movie_file(funcDisp,create_report(‘film.bat‘)))
        rmf.send(os.getcwd())
        rmf.close()
 
    except Exception as exc:    
        print("%s" % exc);   
    print("done");     

  

该脚本增加单个文件read_bdv_index_V3和bdv新格式支持read_bdv_index_V4。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# -*- coding: UTF-8 -*-
 
import os
import io
import sys
import string
import shutil
import codecs
 
 
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
 
 
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
 
def read_bdv_index_V3(objFile):
    piece_list= list()         
    oneFile= os.path.basename(objFile)   
    piece_list.append(oneFile)
    return "avi",piece_list
 
 
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
 
     
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
    # total file count
    cItem = count_file_item(filename,".bdv")
    if(cItem == 1):
        ext_type,piece_list = 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)       
 
 
    
 
def read_bdv_file(filename):
    piece_list= list()
    piece_list.append(‘*.bdv_*‘)
    return  (‘avi‘,piece_list)
 
def read_rmvb_file(filename):
    piece_list= list()
    piece_list.append(‘*.rmvb_*‘)
    return  (‘rmvb‘,piece_list)
 
def read_mkv_file(filename):
    piece_list= list()   
    piece_list.append(‘*.mkv_*‘)
    return  (‘mkv‘,piece_list)
 
def read_mp4_file(filename):
    piece_list= list()   
    piece_list.append(‘*.mp4_*‘)
    return  (‘mp4‘,piece_list)
 
def read_mkv_2_file(filename):
    piece_list= list()   
    piece_list.append(‘video_*‘)
    return  (‘mkv‘,piece_list)

  

 

合并百度影音的离线数据 with python 2.2 bdv格式的更新,布布扣,bubuko.com

合并百度影音的离线数据 with python 2.2 bdv格式的更新

上一篇:python 购物数据意淫分析(1)


下一篇:JAVA--异常