-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.py
72 lines (59 loc) · 2.21 KB
/
encode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from zfec import filefec
import os, sys
"""
Encodes a file into chunks.
Important: chunk_size: specifies size of chunk.
"""
def split_and_encode(filestr, k, n):
chunk_size = 10 * 3 * 1024 * 1024 / 8 # Size of Frame (10s each)
chunk_num = 1
filename = ''
# Remove "file-" prefix and extension.
try:
filename=(((filestr.split('.'))[0]).split('file-'))[1]
except:
print "Filename in wrong format. Must be 'file-<filename>.ext."
return
dirname = 'chunks-' + filename
os.mkdir(dirname)
forig = open(filestr, 'rb')
data = forig.read(chunk_size)
while data:
subfilestr = filename.replace('_', '-') + '.' + str(chunk_num)
subfile = open(dirname + '/' + subfilestr, 'wb')
subfile.write(data)
subfile.close()
code(subfilestr, subfilestr, k, n, dirname)
data = forig.read(chunk_size)
chunk_num += 1
forig.close()
def code(filestr, prefix, k, n, dirname=''):
"""Makes a directory for the specified file portion to code and stores
encoded packets into that directory."""
print "filestr", filestr, "prefix", prefix, "dirname", dirname
current_dir = os.getcwd()
os.chdir(dirname)
os.mkdir(filestr + '.dir')
f = open(filestr, 'rb')
f.seek(0,2) # Seek to 0th bit from the end of the file (second param = 2)
file_size = f.tell() # See what's the index of the last bit, or the filesize
f.seek(0,0) # Seek to 0th bit of the file (second param = 0)
# Call filefec's encode_to_files
# parameters : File / File Size / Target Directory / File Name / k / n / File Extension
filefec.encode_to_files(f, file_size, filestr + '.dir', prefix, k, n, '.chunk')
os.chdir(current_dir)
if __name__ == "__main__":
"""Encodes files within the movies directory."""
k = 20
n = 40
if len(sys.argv) < 2:
print "Usage: python encode.py <filename> <chunk> <coded chunks>"
print "Defaults: <chunk=20> <coded chunks=40>"
else:
filestr = sys.argv[1]
if len(sys.argv) == 4:
k = int(sys.argv[2])
n = int(sys.argv[3])
# movies_path = '/home/ec2-user/movies'
# os.chdir(movies_path)
split_and_encode(filestr, k, n)