-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathpyconcat.py
More file actions
72 lines (58 loc) · 2.14 KB
/
Copy pathpyconcat.py
File metadata and controls
72 lines (58 loc) · 2.14 KB
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
#!/usr/bin/env python
from pyAvTranscoder import avtranscoder as av
from sets import Set
# Get command line arguments
args = []
try:
# python2.7+
import argparse
# Create command-line interface
parser = argparse.ArgumentParser(
prog='pyconcat',
description='''Concatenate first stream of each given file to create an output file.''',
)
# requirements
parser.add_argument('inputs', nargs='+', action='store', help='list of files to concatenate')
# options
parser.add_argument("-o", "--outputFile", dest="outputFileName", type=str, default="output.mov", help="Set the output filename (output.mov by default).")
# Parse command-line
args = parser.parse_args()
except ImportError:
print("pyconcat currently expects python2.7+")
exit(1)
# setup avtranscoder
logger = av.Logger().setLogLevel(av.AV_LOG_QUIET)
av.preloadCodecsAndFormats()
streamTypeToConcat = Set()
codecToConcat = Set()
# get all input files
inputFiles = []
for input in args.inputs:
inputFile = av.InputFile(input)
streamTypeToConcat.add( inputFile.getStream(0).getProperties().getStreamType() )
codecToConcat.add( inputFile.getStream(0).getProperties().getCodecName() )
inputFiles.append(inputFile)
# Check type of streams to rewrap
if len(streamTypeToConcat) > 1:
raise RuntimeError("Cannot concatenate streams of different type.")
if len(codecToConcat) > 1:
raise RuntimeError("Cannot concatenate streams of different codec: ", [codec for codec in codecToConcat])
# Create the output
outputFile = av.OutputFile( args.outputFileName );
if av.AVMEDIA_TYPE_VIDEO in streamTypeToConcat:
outputFile.addVideoStream( inputFiles[-1].getStream(0).getVideoCodec() )
elif av.AVMEDIA_TYPE_AUDIO in streamTypeToConcat:
outputFile.addVideoStream( inputFiles[-1].getStream(0).getAudioCodec() )
### process
outputFile.beginWrap()
data = av.Frame()
# for each input
for inputFile in inputFiles:
packetRead = True
# read all packets of first stream
while packetRead:
# read
packetRead = inputFile.readNextPacket( data, 0 )
# wrap
outputFile.wrap( data, 0 )
outputFile.endWrap()