-
Notifications
You must be signed in to change notification settings - Fork 3
/
extract_moving_picture.py
executable file
·86 lines (73 loc) · 2.9 KB
/
extract_moving_picture.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
import os
import sys, getopt
def extract(filename, extractImage):
with open(filename, "rb") as binary_file:
name, ext = os.path.splitext(binary_file.name)
binary_file.seek(0, os.SEEK_END)
num_bytes = binary_file.tell() # Get the file size
jpg_found = False
for i in range(num_bytes):
binary_file.seek(i)
if extractImage and not jpg_found:
jpg_end_bytes = binary_file.read(4)
if jpg_end_bytes == b"\xFF\xD9\x00\x00": # JPG end
# Go back to beginning of the file and extract the jpg
binary_file.seek(0)
jpg_data = binary_file.read(i + 4)
with open("%s_i.jpg" %(name), "wb") as outfile:
outfile.write(jpg_data)
jpg_found = True
else:
mp4_start_bytes = binary_file.read(16)
if mp4_start_bytes == b"\x00\x00\x00\x18\x66\x74\x79\x70\x6D\x70\x34\x32\x00\x00\x00\x00": # MP4 Start
# Go to to beginning of the mp4 file and extract
binary_file.seek(i)
mp4_data = binary_file.read(num_bytes - i)
with open("%s.mp4" %(name), "wb") as outfile:
outfile.write(mp4_data)
break
def usage():
print('extract_moving_picture.py -i <file>')
print('extract_moving_picture.py -f <folder>\n')
print('extract_moving_picture.py -i <file> -e')
print('extract_moving_picture.py -f <folder> -e\n')
print('when you append -e the original image is also extracted')
if __name__ == "__main__":
print("Huawei Moving Picture / Momente Extractor\n")
if sys.version_info[0] < 3:
print("This script requires Python 3")
sys.exit(-1)
filename = None
extractImage = False
isFolder = False
try:
opts, args = getopt.getopt(sys.argv[1:],"hi:f:e",["ifile=, folder="])
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
usage()
sys.exit()
elif opt in ("-i", "--ifile"):
filename = arg
elif opt in ("-f", "--folder"):
filename = arg
isFolder = True
elif opt == "-e":
extractImage = True
if filename is None:
usage()
sys.exit(2)
if not isFolder:
print('Extract MP4 from file "%s"' %(filename))
extract(filename, extractImage)
else:
foldername = os.path.join(os.path.abspath("."), filename)
print('Extract MP4 from folder "%s"' %(foldername))
for root, dirs, files in os.walk(foldername):
for file_ in files:
filename = os.path.join(root, file_)
print('Extract MP4 from file "%s"' %(filename))
extract(filename, extractImage)