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

467 views
Skip to first unread message

redsto...@163.com

unread,
May 7, 2013, 7:55:14 AM5/7/13
to perian-...@googlegroups.com
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 

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 5 digits 
    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) 

Graham Booker

unread,
May 7, 2013, 9:44:09 AM5/7/13
to perian-...@googlegroups.com
It looks like only the first part will contain the flv header.  Without this, the file is unplayable.  Even if you included the header, flv cannot be split at arbitrary points.  You must do your split over frame boundaries.



--
You received this message because you are subscribed to the Google Groups "perian discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to perian-discus...@googlegroups.com.
To post to this group, send email to perian-...@googlegroups.com.
Visit this group at http://groups.google.com/group/perian-discuss?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages