我目前正在做一个学校项目,我似乎遇到了 MPEG 文件的一些问题。我的项目范围是:
1) 将 MPEG 文件分割成许多固定大小的块。
2)在省略某些块的同时组装其中一些。
问题1:
当我在媒体播放器中播放文件时,它会播放视频,直到它到达我省略的块。
例子:
chunk = ["yui_1", "yui_2", "yui_3", "yui_5", "yui_6"]
Duration of each chunk: 1 second
*如果您意识到我省略了“yui_4”块。*
如果我要组装除“yui_4”之外的所有块,则视频将先播放 2 秒,然后在整个持续时间内挂起。
问题2:
当我在省略第一个块的同时组装块时,它会使整个 mpeg 文件无法播放。
例子:
chunk = ["yui_2", "yui_3", "yui_4", "yui_5", "yui_6"]
Duration of each chunk: 1 second
以下是我的部分代码(硬代码):
def splitFile(inputFile,chunkSize):
splittext = string.split(filename, ".")
name = splittext[0]
extension = splittext[1]
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 = name + "_%s" % count
chunkNames.append(fn1)
f = open(fn1, 'wb')
f.write(data[i:i+ chunkSize])
count += 1
f.close()
以下是我如何组装块的一部分:
def assemble():
data = ["yui_1", "yui_2", "yui_3", "yui_4", "yui_5", "yui_6", "yui_7"]
output = open("output.mpeg", "wb")
for item in datafile:
data = open(item, "rb"). read()
output.write(data)
output.close()