forked from avTranscoder/avTranscoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyrewrap.py
79 lines (64 loc) · 3.27 KB
/
pyrewrap.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
#!/usr/bin/env python
from pyAvTranscoder import avtranscoder as av
# Get command line arguments
args = []
try:
# python2.7+
import argparse
# Create command-line interface
parser = argparse.ArgumentParser(
prog='pyrewrap',
description='''Rewrap a video file and can move the index (moov atom) to the beginning of the file.''',
)
# requirements
parser.add_argument('inputFileName', type=str, help='It could be any video file. Support file without extension.')
# options
parser.add_argument("-o", "--outputFile", dest="outputFileName", type=str, default="output.mov", help="Set the output filename (output.mov by default).")
parser.add_argument("-c", "--format", dest="format", type=str, default="mov", help="Specify the output format.")
parser.add_argument("-f", "--faststart", dest="faststart", action="store_true", default=False, help="Specify if the faststart option must be apply during rewrapping process (warning: 'mov' specific option).")
# Parse command-line
args = parser.parse_args()
except ImportError:
import optparse
# Create command-line interface
parser = optparse.OptionParser(
usage='usage: %prog -o <outputfile> -c <format> [-f] -i <inputfile>',
prog='pyrewrap',
description='''Rewrap a video file and can move the index (moov atom) to the beginning of the file.''',
)
# requirements
parser.add_option("-i", "--inputFile", dest='inputFileName', type="string", help='It could be any video file. Support file without extension.')
# options
parser.add_option("-o", "--outputFile", dest="outputFileName", type="string", default="output.mov", help="Set the output filename (output.mov by default).")
parser.add_option("-c", "--format", dest="format", type="string", default="mov", help="Specify the output format.")
parser.add_option("-f", "--faststart", dest="faststart", action="store_true", default=False, help="Specify if the faststart option must be apply during rewrapping process (warning: 'mov' specific option).")
# Parse command-line
args, other = parser.parse_args()
if args.inputFileName is None:
parser.print_help()
exit(1)
# setup avtranscoder
av.Logger().setLogLevel(av.AV_LOG_QUIET)
av.preloadCodecsAndFormats()
# create input file
inputFile = av.InputFile(args.inputFileName)
if len(inputFile.getProperties().getVideoProperties()) == 0:
print("No video stream found in file ", args.inputFileName)
exit(1)
# create output file (need to set format profile of encoding to force output format to mp4)
formatProfile = av.ProfileMap()
formatProfile[ av.avProfileIdentificator ] = "mp4WrapFormatPreset"
formatProfile[ av.avProfileIdentificatorHuman ] = "MP4 rewraping format preset"
formatProfile[ av.avProfileType ] = av.avProfileTypeFormat
formatProfile[ av.avProfileFormat ] = args.format
if args.faststart:
formatProfile[ "movflags" ] = "+faststart"
outputFile = av.OutputFile( args.outputFileName )
outputFile.setupWrapping( formatProfile )
# create transcoder
transcoder = av.Transcoder( outputFile )
for streamIndex in range(0, inputFile.getProperties().getNbStreams()):
transcoder.addStream(av.InputStreamDesc(args.inputFileName, streamIndex))
# launch process
progress = av.ConsoleProgress()
transcoder.process(progress)