Skip to content

Commit b66ec81

Browse files
update cloud examples
1 parent 10a7ae0 commit b66ec81

6 files changed

Lines changed: 110 additions & 69 deletions

File tree

examples/clouds/aws_cloud.py

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
examples.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
1010
1111
:license: MIT, see LICENSE for more details.
@@ -21,12 +21,16 @@
2121
from os.path import isfile, join
2222

2323
import boto3
24-
import botocore
24+
from botocore.exceptions import ClientError
2525

2626
import ffmpeg_streaming
2727
from 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+
3034
class 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

6477
def 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-
8494
def 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):
95105
def 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("\rTranscoding...(%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

examples/clouds/azure_cloud.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Open a file from a Microsoft Azure 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
1010
1111
:license: MIT, see LICENSE for more details.
@@ -26,30 +26,53 @@
2626
from ffmpeg_streaming import Clouds
2727

2828

29+
logging.basicConfig(filename='streaming.log', level=logging.NOTSET, format='[%(asctime)s] %(levelname)s: %(message)s')
30+
start_time = time.time()
31+
32+
2933
class MicrosoftAzure(Clouds):
3034
def __init__(self, **options):
3135
self.block_blob_service = BlockBlobService(**options)
3236

3337
def upload_directory(self, directory, **options):
34-
files = [f for f in listdir(directory) if isfile(join(directory, f))]
3538
container = options.pop('container', None)
36-
for file in files:
37-
self.block_blob_service.create_blob_from_path(container, file, join(directory, file))
39+
if container is None:
40+
raise ValueError('You should pass a container name')
3841

39-
def download(self, filename=None, **options):
40-
if filename is None:
41-
tmp = tempfile.NamedTemporaryFile(suffix='_py_ff_vi_st.tmp', delete=False)
42-
filename = tmp.name
42+
files = [f for f in listdir(directory) if isfile(join(directory, f))]
43+
44+
try:
45+
for file in files:
46+
self.block_blob_service.create_blob_from_path(container, file, join(directory, file))
47+
except:
48+
error = "An error occurred while uploading the directory"
49+
logging.error(error)
50+
raise RuntimeError(error)
4351

52+
def download(self, filename=None, **options):
4453
container = options.pop('container', None)
4554
blob = options.pop('blob', None)
4655

47-
self.block_blob_service.get_blob_to_path(container, blob, filename)
56+
if container is None or blob is None:
57+
raise ValueError('You should pass a container name and a blob name')
58+
59+
if filename is None:
60+
with tempfile.NamedTemporaryFile(suffix='_py_ff_vi_st.tmp', delete=False) as tmp:
61+
filename = tmp.name
62+
63+
try:
64+
self.block_blob_service.get_blob_to_path(container, blob, filename)
65+
logging.info("The " + filename + " file was downloaded")
66+
except:
67+
error = "An error occurred while downloading the file"
68+
logging.error(error)
69+
raise RuntimeError(error)
4870

4971
return filename
5072

5173

5274
def azure_cloud(container, blob):
75+
# see https://docs.microsoft.com/en-us/azure/storage/common/storage-account-keys-manage to get credentials
5376
cloud = MicrosoftAzure(account_name='account_name', account_key='account_key')
5477

5578
download_options = {
@@ -66,10 +89,6 @@ def azure_cloud(container, blob):
6689
return from_azure_cloud, to_azure_cloud
6790

6891

69-
logging.basicConfig(filename='streaming.log', level=logging.NOTSET, format='[%(asctime)s] %(levelname)s: %(message)s')
70-
start_time = time.time()
71-
72-
7392
def per_to_time_left(percentage):
7493
if percentage != 0:
7594
diff_time = time.time() - start_time
@@ -84,7 +103,7 @@ def per_to_time_left(percentage):
84103
def transcode_progress(per, ffmpeg):
85104
# You can update a field in your database or log it to a file
86105
# You can also create a socket connection and show a progress bar to users
87-
logging.info(ffmpeg)
106+
# logging.info(ffmpeg)
88107
sys.stdout.write("\rTranscoding...(%s%%) %s [%s%s]" % (per, per_to_time_left(per), '#' * per, '-' * (100 - per)))
89108
sys.stdout.flush()
90109

examples/clouds/cloud.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Open a file from a cloud and save dash 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
1010
1111
:license: MIT, see LICENSE for more details.
@@ -25,6 +25,10 @@
2525
from ffmpeg_streaming import Clouds
2626

2727

28+
logging.basicConfig(filename='streaming.log', level=logging.NOTSET, format='[%(asctime)s] %(levelname)s: %(message)s')
29+
start_time = time.time()
30+
31+
2832
class Cloud(Clouds):
2933
def upload_directory(self, directory, **options):
3034
field_name = options.pop('field_name', None)
@@ -109,10 +113,6 @@ def cloud():
109113
return from_cloud, to_cloud
110114

111115

112-
logging.basicConfig(filename='streaming.log', level=logging.NOTSET, format='[%(asctime)s] %(levelname)s: %(message)s')
113-
start_time = time.time()
114-
115-
116116
def per_to_time_left(percentage):
117117
if percentage != 0:
118118
diff_time = time.time() - start_time

examples/clouds/custom_cloud.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Open a file from a custom cloud and save dash 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
1010
1111
:license: MIT, see LICENSE for more details.
@@ -19,6 +19,10 @@
1919
from ffmpeg_streaming import Clouds
2020

2121

22+
logging.basicConfig(filename='streaming.log', level=logging.NOTSET, format='[%(asctime)s] %(levelname)s: %(message)s')
23+
start_time = time.time()
24+
25+
2226
class CustomCloud(Clouds):
2327

2428
def upload_directory(self, directory, **options):
@@ -46,10 +50,6 @@ def custom_cloud():
4650
return from_custom_cloud, to_custom_cloud
4751

4852

49-
logging.basicConfig(filename='streaming.log', level=logging.NOTSET, format='[%(asctime)s] %(levelname)s: %(message)s')
50-
start_time = time.time()
51-
52-
5353
def per_to_time_left(percentage):
5454
if percentage != 0:
5555
diff_time = time.time() - start_time
@@ -64,7 +64,7 @@ def per_to_time_left(percentage):
6464
def transcode_progress(per, ffmpeg):
6565
# You can update a field in your database or log it to a file
6666
# You can also create a socket connection and show a progress bar to users
67-
logging.info(ffmpeg)
67+
# logging.info(ffmpeg)
6868
sys.stdout.write("\rTranscoding...(%s%%) %s [%s%s]" % (per, per_to_time_left(per), '#' * per, '-' * (100 - per)))
6969
sys.stdout.flush()
7070

examples/clouds/google_cloud.py

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Open a file from a google 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
1010
1111
:license: MIT, see LICENSE for more details.
@@ -26,41 +26,57 @@
2626
from ffmpeg_streaming import Clouds
2727

2828

29+
logging.basicConfig(filename='streaming.log', level=logging.NOTSET, format='[%(asctime)s] %(levelname)s: %(message)s')
30+
start_time = time.time()
31+
32+
2933
class GoogleCloudStorage(Clouds):
30-
def __init__(self, bucket_name, **kwargs):
31-
storage_client = storage.Client(**kwargs)
32-
self.bucket_name = bucket_name
33-
self.bucket = storage_client.get_bucket(bucket_name)
34+
def __init__(self, **options):
35+
self.client = storage.Client(**options)
3436

3537
def upload_directory(self, directory, **options):
38+
bucket_name = options.pop('bucket_name', None)
39+
if bucket_name is None:
40+
raise ValueError('You should pass a bucket name')
41+
42+
bucket = self.client.get_bucket(bucket_name)
43+
3644
files = [f for f in listdir(directory) if isfile(join(directory, f))]
3745

3846
for file in files:
39-
blob = self.bucket.blob(self.bucket_name + file, options)
47+
blob = bucket.blob(bucket_name + file, **options)
4048
blob.upload_from_filename(join(directory, file))
4149

4250
def download(self, filename=None, **options):
51+
bucket_name = options.pop('bucket_name', None)
52+
if bucket_name is None:
53+
raise ValueError('You should pass a bucket name')
54+
55+
bucket = self.client.get_bucket(bucket_name)
56+
4357
if filename is None:
44-
tmp = tempfile.NamedTemporaryFile(suffix='_py_ff_vi_st.tmp', delete=False)
45-
filename = tmp.name
58+
with tempfile.NamedTemporaryFile(suffix='_py_ff_vi_st.tmp', delete=False) as tmp:
59+
filename = tmp.name
4660

4761
object_name = options.pop('object_name', None)
4862
if object_name is None:
4963
raise ValueError('You should pass an object name')
5064

51-
blob = self.bucket.get_blob(object_name, options)
65+
blob = bucket.get_blob(object_name, options)
5266
blob.download_to_filename(filename)
5367

5468
return filename
5569

5670

5771
def google_cloud(bucket_name, object_name):
58-
cloud = GoogleCloudStorage(bucket_name)
72+
# see https://cloud.google.com/storage/docs/authentication to authenticate your service
73+
cloud = GoogleCloudStorage()
5974
download_options = {
60-
'object_name': object_name,
75+
'bucket_name': bucket_name,
76+
'key': object_name,
6177
}
6278
upload_options = {
63-
'encryption': 'BASE64_ENCRYPTION',
79+
'bucket_name': bucket_name,
6480
}
6581

6682
from_google_cloud = (cloud, download_options, None)
@@ -69,10 +85,6 @@ def google_cloud(bucket_name, object_name):
6985
return from_google_cloud, to_google_cloud
7086

7187

72-
logging.basicConfig(filename='streaming.log', level=logging.NOTSET, format='[%(asctime)s] %(levelname)s: %(message)s')
73-
start_time = time.time()
74-
75-
7688
def per_to_time_left(percentage):
7789
if percentage != 0:
7890
diff_time = time.time() - start_time
@@ -87,7 +99,7 @@ def per_to_time_left(percentage):
8799
def transcode_progress(per, ffmpeg):
88100
# You can update a field in your database or log it to a file
89101
# You can also create a socket connection and show a progress bar to users
90-
logging.info(ffmpeg)
102+
# logging.info(ffmpeg)
91103
sys.stdout.write("\rTranscoding...(%s%%) %s [%s%s]" % (per, per_to_time_left(per), '#' * per, '-' * (100 - per)))
92104
sys.stdout.flush()
93105

examples/clouds/multiple_clouds.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Open a file from a local path and save dash files to multiple clouds
66
77
8-
:copyright: (c) 2019 by Amin Yazdanpanah.
8+
:copyright: (c) 2020 by Amin Yazdanpanah.
99
:website: https://www.aminyazdanpanah.com
1010
1111
:license: MIT, see LICENSE for more details.
@@ -39,7 +39,7 @@ def per_to_time_left(percentage):
3939
def transcode_progress(per, ffmpeg):
4040
# You can update a field in your database or log it to a file
4141
# You can also create a socket connection and show a progress bar to users
42-
logging.info(ffmpeg)
42+
# logging.info(ffmpeg)
4343
sys.stdout.write("\rTranscoding...(%s%%) %s [%s%s]" % (per, per_to_time_left(per), '#' * per, '-' * (100 - per)))
4444
sys.stdout.flush()
4545

0 commit comments

Comments
 (0)