Python 視頻檔案的分割和合并

來源:互聯網
上載者:User

標籤:python視頻處理

--分割代碼 start----

import sys,os;

kilobytes = 1024;

megabytes = kilobytes*1024;

chunksize = int(10*megabytes);


def split(fromfile,todir,chunksize=chunksize):

if not os.path.exists(todir):

os.mkdir(todir) 

else:

for fname in os.listdir(todir):

os.remove(os.path.join(todir,fname))


partnum = 0

inputfile = open(fromfile,"rb")

while True:

chunk = inputfile.read(chunksize)

if not chunk:

inputfile.close()

break

partnum += 1

filename = os.path.join(todir, ("abc%04d"%partnum))

print("我要顯示的檔案名稱:"+filename)

fileobj = open(filename, "wb")

fileobj.write(chunk)

fileobj.close()

return partnum



if __name__=="__main__":

fromfile = "F:\\abc.mp4"

todir = "F:\\split_parts\\"

#chunksize = int(5000000)

absfrom,absto = map(os.path.abspath,[fromfile,todir])

print(‘分割:‘,absfrom,‘to‘,absto,‘by‘,chunksize)


try:

parts = split(fromfile,todir,chunksize)

except:

print(‘Error during split:‘)

print(sys.exc_info()[0],sys.exc_info()[1])

else:

print(‘分割完成:‘,parts,‘parts are in‘,absto)


--合并代碼 start----

import sys,os

def joinfile(fromdir,filename,todir):

if not os.path.exists(todir):

os.mkdir(todir)

if not os.path.exists(fromdir):

print("合拼檔案路徑錯誤!")


outfile = open(os.path.join(todir,filename),"wb")

files = os.listdir(fromdir)

files.sort()

for file in files:

filepath = os.path.join(fromdir,file)

infile = open(filepath,"rb")

data = infile.read()

outfile.write(data)

infile.close()


outfile.close()



if __name__=="__main__":

fromdir = "F:\\split_parts\\"

todir = "F:\\split_parts\\"

filename = "abc.mp4"


try:

joinfile(fromdir,filename,todir)

except:

print("錯誤的串連檔案:")

print(sys.exc_info()[0],sys.exc_info()[1])


Python 視頻檔案的分割和合并

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.