22examples.clouds.aws_cloud
33~~~~~~~~~~~~
44
5- Open a file from a Amazon S3 cloud and save dash files to it
5+ Open a file from a Amazon S3 cloud and save hls files to it
66
77
8- :copyright: (c) 2019 by Amin Yazdanpanah.
8+ :copyright: (c) 2020 by Amin Yazdanpanah.
99:website: https://www.aminyazdanpanah.com
10101111:license: MIT, see LICENSE for more details.
2121from os .path import isfile , join
2222
2323import boto3
24- import botocore
24+ from botocore . exceptions import ClientError
2525
2626import ffmpeg_streaming
2727from ffmpeg_streaming import Clouds
2828
2929
30+ logging .basicConfig (filename = 'streaming.log' , level = logging .NOTSET , format = '[%(asctime)s] %(levelname)s: %(message)s' )
31+ start_time = time .time ()
32+
33+
3034class AWS (Clouds ):
3135 def __init__ (self , ** options ):
3236 self .s3 = boto3 .client ('s3' , ** options )
@@ -37,32 +41,42 @@ def upload_directory(self, directory, **options):
3741 raise ValueError ('You should pass a bucket name' )
3842
3943 files = [f for f in listdir (directory ) if isfile (join (directory , f ))]
40- for file in files :
41- self .s3 .upload_file (join (directory , file ), bucket_name , file )
4244
43- def download (self , filename = None , ** options ):
44- if filename is None :
45- tmp = tempfile .NamedTemporaryFile (suffix = '_py_ff_vi_st.tmp' , delete = False )
46- filename = tmp .name
45+ try :
46+ for file in files :
47+ self .s3 .upload_file (join (directory , file ), bucket_name , file )
48+ except ClientError as e :
49+ logging .error (e )
50+ raise RuntimeError (e )
51+
52+ logging .info ("The " + directory + "directory was uploaded to Amazon S3 successfully" )
4753
54+ def download (self , filename = None , ** options ):
4855 bucket_name = options .pop ('bucket_name' , None )
4956 key = options .pop ('key' , None )
57+
5058 if bucket_name is None or key is None :
5159 raise ValueError ('You should pass a bucket name and a key' )
5260
61+ if filename is None :
62+ filename = tempfile .NamedTemporaryFile (suffix = '_' + key + '_py_ff_vi_st.tmp' , delete = False )
63+ else :
64+ filename = open (filename , 'wb' )
65+
5366 try :
54- self . s3 . Bucket ( bucket_name ). download_file ( key , filename )
55- except botocore . exceptions . ClientError as e :
56- if e . response [ 'Error' ][ 'Code' ] == "404" :
57- raise RuntimeError ( "The object does not exist." )
58- else :
59- raise RuntimeError ("Could not connect to the server" )
67+ with filename as f :
68+ self . s3 . download_fileobj ( bucket_name , key , f )
69+ logging . info ( "The " + filename . name + " file was downloaded" )
70+ except ClientError as e :
71+ logging . error ( e )
72+ raise RuntimeError (e )
6073
61- return filename
74+ return filename . name
6275
6376
6477def aws_cloud (bucket_name , key ):
65- cloud = AWS (aws_access_key_id = 'YOUR_KEY_ID' , aws_secret_access_key = 'YOUR_KEY_SECRET' )
78+ # see https://docs.aws.amazon.com/general/latest/gr/aws-security-credentials.html for getting Security Credentials
79+ cloud = AWS (aws_access_key_id = 'YOUR_KEY_ID' , aws_secret_access_key = 'YOUR_KEY_SECRET' , region_name = 'YOUR_REGION' )
6680 download_options = {
6781 'bucket_name' : bucket_name ,
6882 'key' : key ,
@@ -77,10 +91,6 @@ def aws_cloud(bucket_name, key):
7791 return from_aws_cloud , to_aws_cloud
7892
7993
80- logging .basicConfig (filename = 'streaming.log' , level = logging .NOTSET , format = '[%(asctime)s] %(levelname)s: %(message)s' )
81- start_time = time .time ()
82-
83-
8494def per_to_time_left (percentage ):
8595 if percentage != 0 :
8696 diff_time = time .time () - start_time
@@ -95,7 +105,7 @@ def per_to_time_left(percentage):
95105def transcode_progress (per , ffmpeg ):
96106 # You can update a field in your database or log it to a file
97107 # You can also create a socket connection and show a progress bar to users
98- logging .info (ffmpeg )
108+ # logging.info(ffmpeg)
99109 sys .stdout .write ("\r Transcoding...(%s%%) %s [%s%s]" % (per , per_to_time_left (per ), '#' * per , '-' * (100 - per )))
100110 sys .stdout .flush ()
101111
@@ -112,10 +122,10 @@ def main():
112122
113123 (
114124 ffmpeg_streaming
115- .dash (from_aws_cloud , adaption = '"id=0,streams=v id=1,streams=a"' )
116- .format ('libx265 ' )
125+ .hls (from_aws_cloud )
126+ .format ('libx264 ' )
117127 .auto_rep ()
118- .package ('/var/www/media/stream.mpd' , to_aws_cloud , progress = transcode_progress )
128+ .package (clouds = to_aws_cloud , progress = transcode_progress )
119129 )
120130
121131
0 commit comments