use python to split a video file into a set of parts

I

iMath

I use the following python code to split a FLV video file into a set of parts ,when finished ,only the first part video can be played ,the other partsare corrupted.I wonder why and Is there some correct ways to split video files

import sys, os
kilobytes = 1024
megabytes = kilobytes * 1000
chunksize = int(1.4 * megabytes) # default: roughly a floppy

print(chunksize , type(chunksize ))

def split(fromfile, todir, chunksize=chunksize):
if not os.path.exists(todir): # caller handles errors
os.mkdir(todir) # make dir, read/write parts
else:
for fname in os.listdir(todir): # delete any existing files
os.remove(os.path.join(todir, fname))
partnum = 0
input = open(fromfile, 'rb') # use binary mode on Windows
while True: # eof=empty string from read
chunk = input.read(chunksize) # get next part <= chunksize
if not chunk: break
partnum += 1
filename = os.path.join(todir, ('part{}.flv'.format(partnum)))
fileobj = open(filename, 'wb')
fileobj.write(chunk)
fileobj.close() # or simply open().write()
input.close()
assert partnum <= 9999 # join sort fails if 5digits
return partnum

if __name__ == '__main__':

fromfile = input('File to be split: ') # input if clicked
todir = input('Directory to store part files:')
print('Splitting', fromfile, 'to', todir, 'by', chunksize)
parts = split(fromfile, todir, chunksize)
print('Split finished:', parts, 'parts are in', todir)
 
C

Chris Angelico

I use the following python code to split a FLV video file into a set of parts ,when finished ,only the first part video can be played ,the other parts are corrupted.I wonder why and Is there some correct ways to split videofiles

Most complex files of this nature have headers. You're chunking it in
pure bytes, so chances are you're disrupting that. The only thing you
can reliably do with your chunks is recombine them into the original
file.
import sys, os
kilobytes = 1024
megabytes = kilobytes * 1000
chunksize = int(1.4 * megabytes) # default: roughly afloppy

Hrm. Firstly, this is a very small chunksize for today's files. You
hard-fail any file more than about 13GB, and for anything over a gig,
you're looking at a thousand files or more. Secondly, why are you
working with 1024 at the first level and 1000 at the second? You're
still a smidge short of the 1440KB that was described as 1.44MB, and
you have the same error of unit. Stick to binary kay OR decimal kay,
don't mix and match!
print(chunksize , type(chunksize ))

Since you passed chunksize through the int() constructor, you can be
fairly confident it'll be an int :)
def split(fromfile, todir, chunksize=chunksize):
if not os.path.exists(todir): # caller handles errors
os.mkdir(todir) # make dir, read/writeparts
else:
for fname in os.listdir(todir): # delete any existing files
os.remove(os.path.join(todir, fname))

Tip: Use os.mkdirs() in case some of its parents need to be made. And
if you wrap it in try/catch rather than probing first, you eliminate a
race condition. (By the way, it's pretty dangerous to just delete
files from someone else's directory. I would recommend aborting with
an error if you absolutely must work with an empty directory.)
input = open(fromfile, 'rb') # use binary mode onWindows

As a general rule I prefer to avoid shadowing builtins, but it's not
strictly a problem.
filename = os.path.join(todir, ('part{}.flv'.format(partnum)))
assert partnum <= 9999 # join sort fails if5 digits
return partnum

Why the assertion? Since this is all you do with the partnum, why does
it matter how long the number is? Without seeing the join sort I can't
know why that would fail; but there must surely be a solution to this.
fromfile = input('File to be split: ') # input ifclicked

"clicked"? I'm guessing this is a translation problem, but I've no
idea what you mean by it.

What you have seems to be a reasonably viable (not that I tested it or
anything) file-level split. You should be able to re-join the parts
quite easily. But the subsequent parts are highly unlikely to play.
Even if you were working in a format that had no headers and could
resynchronize, chances are a 1.4MB file won't have enough to play
anything. Consider: A 1280x720 image contains 921,600 pixels;
uncompressed, this would take 2-4 bytes per pixel, depending on color
depth. To get a single playable frame, you would need an i-frame (ie
not a difference frame) to start and end within a single 1.4MB unit;
it would need to compress 50-75% just to fit, and that's assuming
optimal placement. With random placement, you would need to be getting
87% compression on your index frames, and then you'd still get just
one frame inside your chunk. That's not likely to be very playable.

But hey. You can stitch 'em back together again :)

ChrisA
 
D

Dave Angel

I use the following python code to split a FLV video file into a set of parts ,when finished ,only the first part video can be played ,the other parts are corrupted.I wonder why and Is there some correct ways to split video files

There are two parts to answering the question. First, did it accurately
chunk the file into separate pieces. That should be trivial to test --
simply concatenate them back together (eg. using copy /b) and make sure
you get exactly the original. (using md5sum, for example) I think you will.

And second, why the arbitrary pieces don't play in some unspecified
video player. That one's more interesting, but hasn't anything to do
with Python. I'm curious why you would expect that it would play. It
won't have any of the header information, and the compressed data will
be missing its context information. To split apart a binary file into
useful pieces requires a lot of knowledge about the file format.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top