【发布时间】:2012-07-12 06:14:16
【问题描述】:
我目前正在做一个学校项目,我似乎遇到了 MPEG 文件的一些问题。我的项目范围是:
1) 将 MPEG 文件分割成许多固定大小的块。
2) 组装其中一些,同时省略某些块。
问题一:
当我在媒体播放器中播放文件时,它会播放视频,直到到达我省略的块。
例子:
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()
【问题讨论】: