将mpeg文件拆分成许多块,删除某些部分&将它们组装在一起

时间:2012-07-03 10:05:04

标签: python mpeg

  

可能重复:
  python library for splitting video

我试图找出根据用户选择的段大小将mpeg文件拆分成许多不同块的方法。将它们分成块之后,我想删除一些块并组装其余部分。但是,我认为由于mpeg的格式,它不允许我这样做。

有没有人有指导/指导,我应该遵循或解释为什么mpeg被“损坏”当我把它们组装回来并尝试播放它? Python代码赞赏

以下是我将mpeg文件分割成多个块的代码

def splitFile(inputFile,chunkSize):
    os.chdir("./" + media_dir)

    #read the contents of the file
    f = open(inputFile, 'rb')
    data = f.read() # read the entire content of the file
    f.close()

    # get the length of data, ie size of the input file in bytes
    bytes = len(data)

    #calculate the number of chunks to be created
    noOfChunks= bytes/chunkSize
    if(bytes%chunkSize):
        noOfChunks+=1

    #create a info.txt file for writing metadata
    f = open('info.txt', 'w')
    f.write(inputFile+','+'chunk,'+str(noOfChunks)+','+str(chunkSize))
    f.close()

    chunkNames = []
    count = 1
    for i in range(0, bytes+1, chunkSize):
        fn1 = "chunk%s" % count
        chunkNames.append(fn1)
        f = open(fn1, 'wb')
        f.write(data[i:i+ chunkSize])
        count += 1
        f.close()

定义将文件块加入单个文件的功能

def joinFiles(fileName,noOfChunks,chunkSize):

    os.chdir("./media")
    print os.getcwd()
    dataList = []
    count = 1
    for i in range(0,noOfChunks,1):
        chunkName = fileName+'%s'%count
        f = open(chunkName, 'rb')
        dataList.append(f.read())
        count += 1
        f.close()
        f = open(fileName, 'wb')
    for data in dataList:
        f.write(data)
    f.close()

0 个答案:

没有答案