1
|
|
|
""" |
2
|
|
|
examples.dash |
3
|
|
|
~~~~~~~~~~~~ |
4
|
|
|
|
5
|
|
|
Create DASH streams and manifest |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
:copyright: (c) 2020 by Amin Yazdanpanah. |
9
|
|
|
:website: https://www.aminyazdanpanah.com |
10
|
|
|
:email: [email protected] |
11
|
|
|
:license: MIT, see LICENSE for more details. |
12
|
|
|
""" |
13
|
|
|
|
14
|
|
|
import argparse |
15
|
|
|
import datetime |
16
|
|
|
import sys |
17
|
|
|
import logging |
18
|
|
|
|
19
|
|
|
import ffmpeg_streaming |
20
|
|
|
from ffmpeg_streaming import Formats |
21
|
|
|
|
22
|
|
|
logging.basicConfig(filename='streaming.log', level=logging.NOTSET, format='[%(asctime)s] %(levelname)s: %(message)s') |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
def monitor(ffmpeg, duration, time_, time_left, process): |
26
|
|
|
""" |
27
|
|
|
You can monitor ffmpeg command line and handle the process. For example, you can update a field in your database |
28
|
|
|
or log it to a file. |
29
|
|
|
|
30
|
|
|
Examples: |
31
|
|
|
1. Logging or printing ffmpeg command |
32
|
|
|
logging.info(ffmpeg) or print(ffmpeg) |
33
|
|
|
|
34
|
|
|
2. Handling Process object |
35
|
|
|
if "something happened": |
36
|
|
|
process.terminate() |
37
|
|
|
|
38
|
|
|
3. Email someone to notice the time of finishing process |
39
|
|
|
if time_left > 3600 and not already_send: # if it takes more than one hour and you did not email them already |
40
|
|
|
Email.send( |
41
|
|
|
email='[email protected]', |
42
|
|
|
subject='Your video will be ready at %s seconds' % time_left, |
43
|
|
|
message='Your video takes more than an hour ...' |
44
|
|
|
) |
45
|
|
|
already_send = True |
46
|
|
|
|
47
|
|
|
4. Create a socket connection and show a progress bar(and other parameters) to users |
48
|
|
|
Socket.broadcast( |
49
|
|
|
address=127.0.0.1 |
50
|
|
|
port=5050 |
51
|
|
|
data={ |
52
|
|
|
percentage = per, |
53
|
|
|
time_left = datetime.timedelta(seconds=int(time_left)) |
54
|
|
|
} |
55
|
|
|
) |
56
|
|
|
|
57
|
|
|
:param ffmpeg: ffmpeg command line |
58
|
|
|
:param duration: duration of the video |
59
|
|
|
:param time_: current time of transcoded video |
60
|
|
|
:param time_left: seconds left to finish the video process |
61
|
|
|
:param process: subprocess object |
62
|
|
|
:return: None |
63
|
|
|
""" |
64
|
|
|
per = round(time_ / duration * 100) |
65
|
|
|
sys.stdout.write( |
66
|
|
|
"\rTranscoding...(%s%%) %s left [%s%s]" % |
67
|
|
|
(per, datetime.timedelta(seconds=int(time_left)), '#' * per, '-' * (100 - per)) |
68
|
|
|
) |
69
|
|
|
sys.stdout.flush() |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
def main(): |
73
|
|
|
parser = argparse.ArgumentParser() |
74
|
|
|
|
75
|
|
|
parser.add_argument('-i', '--input', required=True, help='The path to the video file (required).') |
76
|
|
|
parser.add_argument('-o', '--output', default=None, help='The output to write files.') |
77
|
|
|
|
78
|
|
|
parser.add_argument('-hls', '--hls_output', default=False, help='publish hls playlists') |
79
|
|
|
|
80
|
|
|
args = parser.parse_args() |
81
|
|
|
|
82
|
|
|
video = ffmpeg_streaming.input(args.input) |
83
|
|
|
|
84
|
|
|
dash = video.dash(Formats.h264()) |
85
|
|
|
dash.auto_generate_representations() |
86
|
|
|
|
87
|
|
|
if args.hls_output: |
88
|
|
|
dash.generate_hls_playlist() |
89
|
|
|
|
90
|
|
|
dash.output(args.output, monitor=monitor) |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
if __name__ == "__main__": |
94
|
|
|
sys.exit(main()) |
95
|
|
|
|