qtfaststart2 1.0__0-py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ VERSION = "1.0.0"
@@ -0,0 +1,2 @@
1
+ from qtfaststart import command
2
+ command.run()
@@ -0,0 +1,83 @@
1
+
2
+ from __future__ import print_function
3
+
4
+ import logging
5
+ import os
6
+ import shutil
7
+ import sys
8
+ import tempfile
9
+
10
+ from optparse import OptionParser
11
+ from qtfaststart import VERSION
12
+ from qtfaststart import processor
13
+ from qtfaststart.exceptions import FastStartException
14
+
15
+ log = logging.getLogger("qtfaststart")
16
+
17
+ def run():
18
+ logging.basicConfig(level = logging.INFO, stream = sys.stdout,
19
+ format = "%(message)s")
20
+
21
+ parser = OptionParser(usage="%prog [options] infile [outfile]",
22
+ version="%prog " + VERSION)
23
+
24
+ parser.add_option("-d", "--debug", dest="debug", default=False,
25
+ action="store_true",
26
+ help="Enable debug output")
27
+ parser.add_option("-l", "--list", dest="list", default=False,
28
+ action="store_true",
29
+ help="List top level atoms")
30
+ parser.add_option("-e", "--to_end", dest="to_end", default=False,
31
+ action="store_true",
32
+ help="Move moov atom to the end of file")
33
+ parser.add_option("-s", "--sample", dest="sample", default=False,
34
+ action="store_true",
35
+ help="Create a small sample of the input file")
36
+
37
+ options, args = parser.parse_args()
38
+
39
+ if len(args) < 1:
40
+ parser.print_help()
41
+ raise SystemExit(1)
42
+
43
+ if options.debug:
44
+ logging.getLogger().setLevel(logging.DEBUG)
45
+
46
+ if options.list:
47
+ index = processor.get_index(open(args[0], "rb"))
48
+
49
+ for atom, pos, size in index:
50
+ if atom == "\x00\x00\x00\x00":
51
+ # Strange zero atom... display with dashes rather than
52
+ # an empty string
53
+ atom = "----"
54
+
55
+ print(atom, "(" + str(size) + " bytes)")
56
+
57
+ raise SystemExit
58
+
59
+ if len(args) == 1:
60
+ # Replace the original file!
61
+ if options.sample:
62
+ print("Please pass an output filename when used with --sample!")
63
+ raise SystemExit(1)
64
+
65
+ tmp, outfile = tempfile.mkstemp()
66
+ os.close(tmp)
67
+ else:
68
+ outfile = args[1]
69
+
70
+ limit = 0
71
+ if options.sample:
72
+ # Create a small sample (4 MiB)
73
+ limit = 4 * (1024 ** 2)
74
+
75
+ try:
76
+ processor.process(args[0], outfile, limit = limit, to_end = options.to_end)
77
+ except FastStartException:
78
+ # A log message was printed, so exit with an error code
79
+ raise SystemExit(1)
80
+
81
+ if len(args) == 1:
82
+ # Move temp file to replace original
83
+ shutil.move(outfile, args[0])
@@ -0,0 +1,23 @@
1
+ class FastStartException(Exception):
2
+ """
3
+ Raised when something bad happens during processing.
4
+ """
5
+ pass
6
+
7
+ class FastStartSetupError(FastStartException):
8
+ """
9
+ Rasised when asked to process a file that does not need processing
10
+ """
11
+ pass
12
+
13
+ class MalformedFileError(FastStartException):
14
+ """
15
+ Raised when the input file is setup in an unexpected way
16
+ """
17
+ pass
18
+
19
+ class UnsupportedFormatError(FastStartException):
20
+ """
21
+ Raised when a movie file is recognized as a format not supported.
22
+ """
23
+ pass
@@ -0,0 +1,330 @@
1
+ """
2
+ The guts that actually do the work. This is available here for the
3
+ 'qtfaststart' script and for your application's direct use.
4
+ """
5
+
6
+ import shutil
7
+ import logging
8
+ import os
9
+ import struct
10
+ import collections
11
+
12
+ import io
13
+
14
+ from qtfaststart.exceptions import FastStartSetupError
15
+ from qtfaststart.exceptions import MalformedFileError
16
+ from qtfaststart.exceptions import UnsupportedFormatError
17
+
18
+ # This exception isn't directly used, included it for backward compatability
19
+ # in the event someone had used it from our namespace previously
20
+ from qtfaststart.exceptions import FastStartException
21
+
22
+ CHUNK_SIZE = 8192
23
+
24
+ log = logging.getLogger("qtfaststart")
25
+
26
+ # Older versions of Python require this to be defined
27
+ if not hasattr(os, 'SEEK_CUR'):
28
+ os.SEEK_CUR = 1
29
+
30
+ Atom = collections.namedtuple('Atom', 'name position size')
31
+
32
+ def read_atom(datastream):
33
+ """
34
+ Read an atom and return a tuple of (size, type) where size is the size
35
+ in bytes (including the 8 bytes already read) and type is a "fourcc"
36
+ like "ftyp" or "moov".
37
+ """
38
+ size, type = struct.unpack(">L4s", datastream.read(8))
39
+ type = type.decode('ascii')
40
+ return size, type
41
+
42
+
43
+ def _read_atom_ex(datastream):
44
+ """
45
+ Read an Atom from datastream
46
+ """
47
+ pos = datastream.tell()
48
+ atom_size, atom_type = read_atom(datastream)
49
+ if atom_size == 1:
50
+ atom_size, = struct.unpack(">Q", datastream.read(8))
51
+ return Atom(atom_type, pos, atom_size)
52
+
53
+
54
+ def get_index(datastream):
55
+ """
56
+ Return an index of top level atoms, their absolute byte-position in the
57
+ file and their size in a list:
58
+
59
+ index = [
60
+ ("ftyp", 0, 24),
61
+ ("moov", 25, 2658),
62
+ ("free", 2683, 8),
63
+ ...
64
+ ]
65
+
66
+ The tuple elements will be in the order that they appear in the file.
67
+ """
68
+ log.debug("Getting index of top level atoms...")
69
+
70
+ index = list(_read_atoms(datastream))
71
+ _ensure_valid_index(index)
72
+
73
+ return index
74
+
75
+
76
+ def _read_atoms(datastream):
77
+ """
78
+ Read atoms until an error occurs
79
+ """
80
+ while datastream:
81
+ try:
82
+ atom = _read_atom_ex(datastream)
83
+ log.debug("%s: %s" % (atom.name, atom.size))
84
+ except:
85
+ break
86
+
87
+ yield atom
88
+
89
+ if atom.size == 0:
90
+ if atom.name == "mdat":
91
+ # Some files may end in mdat with no size set, which generally
92
+ # means to seek to the end of the file. We can just stop indexing
93
+ # as no more entries will be found!
94
+ break
95
+ else:
96
+ # Weird, but just continue to try to find more atoms
97
+ continue
98
+
99
+ datastream.seek(atom.position + atom.size)
100
+
101
+
102
+ def _ensure_valid_index(index):
103
+ """
104
+ Ensure the minimum viable atoms are present in the index.
105
+
106
+ Raise MalformedFileError if not.
107
+ """
108
+ top_level_atoms = set([item.name for item in index])
109
+ for key in ["moov", "mdat"]:
110
+ if key not in top_level_atoms:
111
+ msg = "%s atom not found, is this a valid MOV/MP4 file?" % key
112
+ log.warn(msg)
113
+ raise MalformedFileError(msg)
114
+
115
+
116
+ def find_atoms(size, datastream):
117
+ """
118
+ Compatibilty interface for _find_atoms_ex
119
+ """
120
+ fake_parent = Atom('fake', datastream.tell()-8, size+8)
121
+ for atom in _find_atoms_ex(fake_parent, datastream):
122
+ yield atom.name
123
+
124
+
125
+ def _find_atoms_ex(parent_atom, datastream):
126
+ """
127
+ Yield either "stco" or "co64" Atoms from datastream.
128
+ datastream will be 8 bytes into the stco or co64 atom when the value
129
+ is yielded.
130
+
131
+ It is assumed that datastream will be at the end of the atom after
132
+ the value has been yielded and processed.
133
+
134
+ parent_atom is the parent atom, a 'moov' or other ancestor of CO
135
+ atoms in the datastream.
136
+ """
137
+ stop = parent_atom.position + parent_atom.size
138
+
139
+ while datastream.tell() < stop:
140
+ try:
141
+ atom = _read_atom_ex(datastream)
142
+ except:
143
+ msg = "Error reading next atom!"
144
+ log.exception(msg)
145
+ raise MalformedFileError(msg)
146
+
147
+ if atom.name in ["trak", "mdia", "minf", "stbl"]:
148
+ # Known ancestor atom of stco or co64, search within it!
149
+ for res in _find_atoms_ex(atom, datastream):
150
+ yield res
151
+ elif atom.name in ["stco", "co64"]:
152
+ yield atom
153
+ else:
154
+ # Ignore this atom, seek to the end of it.
155
+ datastream.seek(atom.position + atom.size)
156
+
157
+ def _moov_is_compressed(datastream, moov_atom):
158
+ """
159
+ scan the atoms under the moov atom and detect whether or not the
160
+ atom data is compressed
161
+ """
162
+ # seek to the beginning of the moov atom contents
163
+ datastream.seek(moov_atom.position+8)
164
+
165
+ # step through the moov atom childeren to see if a cmov atom is among them
166
+ stop = moov_atom.position + moov_atom.size
167
+ while datastream.tell() < stop:
168
+ child_atom = _read_atom_ex(datastream)
169
+ datastream.seek(datastream.tell()+child_atom.size - 8)
170
+
171
+ # cmov means compressed moov header!
172
+ if child_atom.name == 'cmov':
173
+ return True
174
+
175
+ return False
176
+
177
+ def process(infilename, outfilename, limit=float('inf'), to_end=False,
178
+ cleanup=True):
179
+ """
180
+ Convert a Quicktime/MP4 file for streaming by moving the metadata to
181
+ the front of the file. This method writes a new file.
182
+
183
+ If limit is set to something other than zero it will be used as the
184
+ number of bytes to write of the atoms following the moov atom. This
185
+ is very useful to create a small sample of a file with full headers,
186
+ which can then be used in bug reports and such.
187
+
188
+ If cleanup is set to False, free atoms and zero atoms will not be
189
+ scrubbed from from the mov
190
+ """
191
+ datastream = open(infilename, "rb")
192
+
193
+ # Get the top level atom index
194
+ index = get_index(datastream)
195
+
196
+ mdat_pos = 999999
197
+ free_size = 0
198
+
199
+ # Make sure moov occurs AFTER mdat, otherwise no need to run!
200
+ for atom in index:
201
+ # The atoms are guaranteed to exist from get_index above!
202
+ if atom.name == "moov":
203
+ moov_atom = atom
204
+ moov_pos = atom.position
205
+ elif atom.name == "mdat":
206
+ mdat_pos = atom.position
207
+ elif atom.name == "free" and atom.position < mdat_pos and cleanup:
208
+ # This free atom is before the mdat!
209
+ free_size += atom.size
210
+ log.info("Removing free atom at %d (%d bytes)" %
211
+ (atom.position, atom.size))
212
+ elif (atom.name == "\x00\x00\x00\x00" and atom.position < mdat_pos):
213
+ # This is some strange zero atom with incorrect size
214
+ free_size += 8
215
+ log.info("Removing strange zero atom at %s (8 bytes)" %
216
+ atom.position)
217
+
218
+ # Offset to shift positions
219
+ offset = - free_size
220
+ if moov_pos < mdat_pos:
221
+ if to_end:
222
+ # moov is in the wrong place, shift by moov size
223
+ offset -= moov_atom.size
224
+ else:
225
+ if not to_end:
226
+ # moov is in the wrong place, shift by moov size
227
+ offset += moov_atom.size
228
+
229
+ if offset == 0:
230
+ # No free atoms to process and moov is correct, we are done!
231
+ msg = "This file appears to already be setup!"
232
+ log.error(msg)
233
+ raise FastStartSetupError(msg)
234
+
235
+ # Check for compressed moov atom
236
+ is_compressed = _moov_is_compressed(datastream, moov_atom)
237
+ if is_compressed:
238
+ msg = "Movies with compressed headers are not supported"
239
+ log.error(msg)
240
+ raise UnsupportedFormatError(msg)
241
+
242
+ # read and fix moov
243
+ moov = _patch_moov(datastream, moov_atom, offset)
244
+
245
+ log.info("Writing output...")
246
+ outfile = open(outfilename, "wb")
247
+
248
+ # Write ftype
249
+ for atom in index:
250
+ if atom.name == "ftyp":
251
+ log.debug("Writing ftyp... (%d bytes)" % atom.size)
252
+ datastream.seek(atom.position)
253
+ outfile.write(datastream.read(atom.size))
254
+
255
+ if not to_end:
256
+ _write_moov(moov, outfile)
257
+
258
+ # Write the rest
259
+ skip_atom_types = ["ftyp", "moov"]
260
+ if cleanup:
261
+ skip_atom_types += ["free"]
262
+
263
+ atoms = [item for item in index if item.name not in skip_atom_types]
264
+ for atom in atoms:
265
+ log.debug("Writing %s... (%d bytes)" % (atom.name, atom.size))
266
+ datastream.seek(atom.position)
267
+
268
+ # for compatability, allow '0' to mean no limit
269
+ cur_limit = limit or float('inf')
270
+ cur_limit = min(cur_limit, atom.size)
271
+
272
+ for chunk in get_chunks(datastream, CHUNK_SIZE, cur_limit):
273
+ outfile.write(chunk)
274
+
275
+ if to_end:
276
+ _write_moov(moov, outfile)
277
+
278
+ # Close and set permissions
279
+ outfile.close()
280
+ try:
281
+ shutil.copymode(infilename, outfilename)
282
+ except:
283
+ log.warn("Could not copy file permissions!")
284
+
285
+ def _write_moov(moov, outfile):
286
+ # Write moov
287
+ bytes = moov.getvalue()
288
+ log.debug("Writing moov... (%d bytes)" % len(bytes))
289
+ outfile.write(bytes)
290
+
291
+ def _patch_moov(datastream, atom, offset):
292
+ datastream.seek(atom.position)
293
+ moov = io.BytesIO(datastream.read(atom.size))
294
+
295
+ # reload the atom from the fixed stream
296
+ atom = _read_atom_ex(moov)
297
+
298
+ for atom in _find_atoms_ex(atom, moov):
299
+ # Read either 32-bit or 64-bit offsets
300
+ ctype, csize = dict(
301
+ stco=('L', 4),
302
+ co64=('Q', 8),
303
+ )[atom.name]
304
+
305
+ # Get number of entries
306
+ version, entry_count = struct.unpack(">2L", moov.read(8))
307
+
308
+ log.info("Patching %s with %d entries" % (atom.name, entry_count))
309
+
310
+ entries_pos = moov.tell()
311
+
312
+ struct_fmt = ">%(entry_count)s%(ctype)s" % vars()
313
+
314
+ # Read entries
315
+ entries = struct.unpack(struct_fmt, moov.read(csize * entry_count))
316
+
317
+ # Patch and write entries
318
+ offset_entries = [entry + offset for entry in entries]
319
+ moov.seek(entries_pos)
320
+ moov.write(struct.pack(struct_fmt, *offset_entries))
321
+ return moov
322
+
323
+ def get_chunks(stream, chunk_size, limit):
324
+ remaining = limit
325
+ while remaining:
326
+ chunk = stream.read(min(remaining, chunk_size))
327
+ if not chunk:
328
+ return
329
+ remaining -= len(chunk)
330
+ yield chunk
@@ -0,0 +1,17 @@
1
+ #!python
2
+
3
+ """
4
+ Command line script for convenience. If this is in your path, you should
5
+ be able to run it directly like this::
6
+
7
+ qtfaststart2
8
+ """
9
+
10
+ import sys
11
+ import os
12
+
13
+ # Add parent directory to sys.path so that running from dev environment works
14
+ sys.path.append(os.path.dirname(os.path.dirname((os.path.abspath(__file__)))))
15
+
16
+ from qtfaststart import command
17
+ command.run()
@@ -0,0 +1,7 @@
1
+ # Released under MIT License
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,23 @@
1
+ Metadata-Version: 2.1
2
+ Name: qtfaststart2
3
+ Version: 1.0.0
4
+ Summary: Quicktime atom positioning in Python for fast streaming.
5
+ Home-page: https://github.com/danielyaa5/qtfaststart2
6
+ Author: danielyaa5
7
+ Author-email: danyagithub@gmail.com
8
+ License: MIT License
9
+ Download-URL: https://github.com/danielyaa5/qtfaststart2/archive/1.0.0.tar.gz
10
+ Platform: any
11
+ Classifier: Development Status :: 5 - Production/Stable
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: License :: OSI Approved :: MIT License
14
+ Classifier: Natural Language :: English
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: Programming Language :: Python
17
+ Classifier: Topic :: Multimedia :: Video :: Conversion
18
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
19
+ Provides: qtfaststart2
20
+
21
+ UNKNOWN
22
+
23
+
@@ -0,0 +1,11 @@
1
+ qtfaststart2/__init__.py,sha256=lzGFsymf0DtA_1oAZcPbeQ557iY-1BRkekhfA2qaFh8,18
2
+ qtfaststart2/__main__.py,sha256=I9PnhEakdAUw43UDy5sRuhAUDWw3urQgIrFlrqP1UJk,46
3
+ qtfaststart2/command.py,sha256=iz2pgKIRvd8HwttYl5ufiUPoEWHBoOcw8JOOyFupUTs,2536
4
+ qtfaststart2/exceptions.py,sha256=YL49nfP_ExBH1x65_uclYqkTFbXjTS6xuW4hYbGAJdc,542
5
+ qtfaststart2/processor.py,sha256=eeBMAiIPFfVmvCTwt322Vh-qoSOdS9irqQZhqj7ZLag,10260
6
+ qtfaststart2-1.0.0.data/scripts/qtfaststart2,sha256=o-qGXe1pnnHU-nQj1_B-gIGYcJLaB61g-DMKzUlNAfA,387
7
+ qtfaststart2-1.0.0.dist-info/LICENSE.md,sha256=cZveWJKN2VzG8ydVFkoDJdmD94rHfkPVQar68dyiDtw,1053
8
+ qtfaststart2-1.0.0.dist-info/METADATA,sha256=eCDSvQy7-rW77px9Be9p6xYhwVH6dmM2TEIWPXJx1MI,787
9
+ qtfaststart2-1.0.0.dist-info/WHEEL,sha256=OqRkF0eY5GHssMorFjlbTIq072vpHpF60fIQA6lS9xA,92
10
+ qtfaststart2-1.0.0.dist-info/top_level.txt,sha256=ZPjPSokyUN5nuntJns2SqKL2U-X5HbfBd5nCKjVzGRs,13
11
+ qtfaststart2-1.0.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: bdist_wheel (0.36.2)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ qtfaststart2