-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjoin_stream.py
More file actions
77 lines (58 loc) · 2.27 KB
/
join_stream.py
File metadata and controls
77 lines (58 loc) · 2.27 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
73
74
75
76
77
#!/usr/bin/env python3
import logging
import os
import tempfile
import click
from ffmpy import FFmpeg
# Logger
logger = logging.getLogger("join_stream")
def setuplog(verbose):
"""Config the log output of join_stream"""
log_msg_format = '%(asctime)s :: %(levelname)5s :: %(name)10s :: %(message)s'
log_date_format = '%Y-%m-%d %H:%M:%S'
logging.basicConfig(format=log_msg_format, datefmt=log_date_format)
if verbose:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.INFO)
@click.command()
@click.option('--input', default="./", type=click.Path(exists=True),
help="Input directory containing downloaded videos")
@click.option('--output', default='outmerge.mp4', help='Output Video File')
@click.option('--verbose', is_flag=True, help="Verbose")
def join_stream(input, output, verbose):
"""Join a list of downloaded TS video files from a given directory (output)
into an Output Video File (output)."""
setuplog(verbose)
# Create temp file to store all videos
tfd, tpath = tempfile.mkstemp(suffix=".ts")
logger.debug("Created temp file at: " + tpath)
with open(tpath, "wb") as tfile:
# Reading video files in input directory
dirfilenames = sorted(os.listdir(input))
logger.info("Reading input files...")
for videofname in dirfilenames:
if videofname.endswith(".ts"):
logger.debug("Reading file: " + videofname)
with open(os.path.join(input, videofname), "rb") as videofile:
for line in videofile:
tfile.write(line)
logger.debug("Finished reading file: " + videofname)
logger.info("Finished reading all video TS files")
tfile.flush()
os.fsync(tfile.fileno())
ff = FFmpeg(
global_options=[
'-y',
'-loglevel error'
],
inputs={tpath: None},
outputs={output: '-acodec copy -vcodec copy'}
)
logger.info("Running FFMPEG tool to convert the file")
logger.debug("FFMPEG CLI: " + ff.cmd)
ff.run()
logger.debug("Finished FFMPEG conversion")
logger.info("Output video file: " + output)
if __name__ == '__main__':
join_stream()