0

I have a video file and i need to divide it into several smaller files of size 256KB and save all files names in a text file then i need to read all the small files and merges them into the original file.

is this possible to do it in python and how ?

5
  • Probably yes. Anything concrete? Commented Oct 8, 2016 at 15:46
  • 1
    It is possible with Python, but there exist programs that already do that for you: split and cat Commented Oct 8, 2016 at 15:47
  • Do the small files need to be readable as video? If you just split the big file into chunks, they won't be. Commented Oct 8, 2016 at 15:48
  • @AndreaCorbellini Thank you for your reply. I know about that but i need to do it using python. Commented Oct 8, 2016 at 15:50
  • @JohnGordon No, i don't need the small files to be readable as video. i just need to split the big file into several files of size 256KB . Commented Oct 8, 2016 at 15:52

1 Answer 1

2

First stab at splitting:

input_file = open(input_filename, 'rb')
blocksize = 4096
chunksize = 1024 * 256
buf = None
chunk_num = 0
current_read = 0
output_filename = 'output-chunk-{:04d}'.format(chunk_num)
output_file = open(output_filename, 'wb')
while buf is None or len(buf) > 0:
    buf = input_file.read(blocksize)
    current_read += len(buf)
    output_file.write(buf)
    if chunksize <= current_read:
        output_file.close()
        current_read = 0
        chunk_num += 1
        output_filename = 'output-chunk-{:04d}'.format(chunk_num)
        output_file = open(output_filename, 'wb')
output_file.close()
input_file.close()

This might get you partway there; adapt as needed.

Merging:

blocksize = 4096
chunk_num = 0
input_filename = 'output-chunk-{:04d}'.format(chunk_num)
output_filename = 'reconstructed.bin'
output_file = open(output_filename, 'wb')
while True:
    try:
        input_file = open(input_filename, 'rb')
    except IOError:
        break
    buf = None
    while buf is None or len(buf) > 0:
        buf = input_file.read(blocksize)
        output_file.write(buf)
    input_file.close()
    chunk_num += 1
    input_filename = 'output-chunk-{:04d}'.format(chunk_num)
output_file.close()
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your answer and it's work great but how i can read all files and merges them into the original file?
Um, cat output-chunk-* > reconstructed.bin.
sorry i can't understand your comment! How i can merges them using python?
Apologies, I didn't fully read your question. My comment was echoing the sentiment from @AndreaCorbellini which is don't do splitting and merging in python if you can help it. There's well-developed tools out there that will do the job for you; don't reinvent the wheel.
it's ok. Now i have the full answer. Thank you a lot for helping me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.