Mumamiriro emazuva ano edhijitari, kuendesa zvemhando yepamusoro vhidhiyo mukati memhando dzakasiyana dzemidziyo uye netiweki mamiriro kwakakosha kupfuura nakare kose. Kunyangwe iwe uri kugadzira chikuva chekutepfenyura, online yekudzidza portal, yesocial media app, kana chero application inoda kutambazve vhidhiyo, isina musono vhidhiyo kutenderera kwakakosha kune yakakwana mushandisi ruzivo.
Kuita otomatiki shanduko yevhidhiyo uchishandisa gore-yakavakirwa encoding sevhisi inokutendera iwe kuti ugone kuburitsa inochinja inoyerera, kuve nechokwadi chemhando yepamusoro yevhidhiyo yevashandisi vako uku uchidzikisa kuoma kwezvivakwa. Ngationgororei kuti ungaite sei mhinduro iyi kuti isangane nezvido zvekushandisa kwako. Chinangwa chedu ndechekugadzira mhinduro iri nyore kuendesa, isingade kugadzirisa, uye inotsigira inokura mushandisi base.
Tichatarisa paHLS (HTTP Live Streaming), imwe yeanonyanya kufarirwa mavhidhiyo ekutepfenyura mapuroteni. HLS inoshandisa encoding uye segmentation: iyo yekutanga vhidhiyo yakavharirwa mune akawanda mavhezheni ane akasiyana bitrate uye zvigadziriso. Aya ma encoded vhezheni anozopatsanurwa kuita machunks madiki, anowanzo 2 kusvika gumi masekonzi pakureba, nechunk yega yega inochengetwa sefaira rega. Mazita ekutamba anozogadzirwa: kutanga, chinyorwa chekutamba cheyega yega encoded yevhidhiyo inogadzirwa, ine maURL emachunks ega ega.
Tevere, imwe chete master playlist inogadzirwa, ichinongedza akasiyana mavhezheni erukova (uye anoenderana playlists) pamwe chete nemagadzirirwo avo uye bitrate. Kuseta uku kunobvumira maapplication emutengi kuti asarudze iyo yakakwana rukova zvichienderana nezvinhu zvakaita seyemutengi gadziriso yemudziyo, saizi yekuona, uye mamiriro etiweki, kuve nechokwadi chekuti zvikamu zvinodiwa chete zvinotorwa.
Kune akawanda maturusi aripo ekugadzira HLS hova kubva kumavhidhiyo. Izvi zvinosanganisira kumhanya FFmpeg zvakananga kana kushandisa makore-based masevhisi kubata shanduko uye kudzivirira manejimendi manejimendi. Mienzaniso yemasevhisi akadai anosanganisira AWS Elemental MediaConvert, Google Cloud Transcoder, Bitmovin, nemamwe. Mune ino post, isu tichatarisa kuMediaConvert. Pazasi pane inogoneka mafambiro ekushandura otomatiki mavhidhiyo akaiswa kuHLS uye kuita kuti nzizi dziwanikwe kune vashandisi. Sezvo iwe uchipfuura kuburikidza nekufamba kwebasa, ndapota tarisa kune yakabatanidzwa dhayagiramu, pane imwe neimwe nhanho yakanyorwa.
Mushandisi anoisa vhidhiyo kubhaketi reS3 achishandisa nharembozha kana yewebhu mutengi app.
Basa reLambda rinokonzerwa neObjectCreate chiitiko muVhidhiyo Inorodha S3 bhaketi. Iri basa rinogadzira basa reMediaConvert richishandisa iyo yakapihwa kumisikidzwa uye yobva yabuda (haimiriri kuti kushandurwa kwevhidhiyo kupedze). Iyo MediaConvert API inopa akasiyana marongero, anosanganisira codec kusarudzwa, bitrate, mhando, odhiyo kugadzirisa, nezvimwe. Inogona zvakare kugadzira akawanda mavhezheni erukova ane akasiyana compression marongero, senge 360p, 720p, 1080p, nezvimwe.
Paunenge uchisarudza iyo encoding yekumisikidza iri kunze kwechikamu cheichi positi, iyo kodhi sampu inosanganisira yakakosha HLS yekurongedza basa ine imwe shanduro pa1 Mbps bitrate. Iyo gadziriso inogona kuwedzerwa nyore nyore kuti isangane nezvinodiwa zvega yega application. Kana dziri mvumo yeIAM, basa iri rinoda kuverenga kuwana kune sosi S3 bhaketi, nyora kupinda kwekuenda S3 bhaketi, uye kuwana kuMediaConvert API.
import boto3 import re output_bucket_name = 'converted-videos-bucket' mediaconvert_role_arn = 'arn:aws:iam::123456789012:role/MediaConvertRole' # output bucket access s3_client = boto3.client('s3') mediaconvert_client = boto3.client('mediaconvert') hls_main_playlist_suffix = '-hls.m3u8' # regex used to normalize the object key for the client request token client_request_token_symbols_to_skip = r'[^a-zA-Z0-9-_]' def lambda_handler(event, context): # get S3 bucket name and object key from the event bucket_name = event['Records'][0]['s3']['bucket']['name'] object_key = event['Records'][0]['s3']['object']['key'] # also used as media id # normalize the object key for the client request token client_request_token_obj_key = re.sub(client_request_token_symbols_to_skip, '_', object_key) # call MediaConvert to transcode the video create_job_response = mediaconvert_client.create_job( Role=mediaconvert_role_arn, ClientRequestToken=client_request_token_obj_key, Settings={ 'Inputs': [ { 'FileInput': f's3://{bucket_name}/{object_key}', 'AudioSelectors': { 'Audio Selector 1': { 'DefaultSelection': 'DEFAULT', }, }, } ], 'OutputGroups': [ { 'Name': 'DefaultOutputGroup', 'OutputGroupSettings': { 'Type': 'HLS_GROUP_SETTINGS', 'HlsGroupSettings': { 'Destination': f's3://{output_bucket_name}/{object_key}-hls', 'DirectoryStructure': 'SUBDIRECTORY_PER_STREAM', 'SegmentLength': 5, 'MinSegmentLength': 2, 'SegmentsPerSubdirectory': 500, 'ProgressiveWriteHlsManifest': 'DISABLED', }, }, 'Outputs': [ { 'NameModifier': '-h264', 'ContainerSettings': { 'Container': 'M3U8', }, 'VideoDescription': { 'CodecSettings': { 'Codec': 'H_264', 'H264Settings': { 'RateControlMode': 'VBR', 'Bitrate': 1000000, }, }, }, 'AudioDescriptions': [ { 'AudioSourceName': 'Audio Selector 1', 'CodecSettings': { 'Codec': 'AAC', 'AacSettings': { 'Bitrate': 96000, 'CodingMode': 'CODING_MODE_2_0', 'SampleRate': 48000, }, }, }, ], }, ], } ], }, ) print('Created a MediaConvert job:', create_job_response) return { 'statusCode': 200, 'body': 'OK', }
MediaConvert inogadzirisa vhidhiyo uye inogadzira HLS playlists uye zvikamu zvevhidhiyo mune inobuda S3 bhaketi. Iyo inobuda bhaketi yakabatana neCDN inochengeta playlists uye zvikamu zvevhidhiyo. Mumuenzaniso uyu, tiri kushandisa Cloudfront asi chero CDN inoenderana neS3 inogona kushandiswa.
Rimwe basa reLambda rinokonzerwa neObjectCreate chiitiko mubhaketi rekubuda. Sefa yezita rechinhu inonamirwa kune icho chinokonzeresa kuti ive nechokwadi chekuti basa rinoshanda chete kana faira rekutamba ragadzirwa (segment mafaera haana hanya).
Iri basa rinowedzera iyo playlist URL kune midhiya rekodhi mudhatabhesi. Iyo yekuchengetera layer inodarika chiyero cheichi positi, saka mune kodhi sampuli, iyo URL inongodhindwa.
import boto3 s3_client = boto3.client('s3') def lambda_handler(event, context): # this function is triggered only when a playlist file # with object key that looks like this '<video_id>-hls.m3u8' # is created in the S3 bucket # get object key from the event object_key = event['Records'][0]['s3']['object']['key'] # extract video id from the object key video_id = object_key.replace('-hls.m3u8', '') print(f'HLS playlist {object_key} created for video {video_id}') # TODO: update the video record in the database return { 'statusCode': 200, 'body': 'OK', }
Kana vashandisi vakavhura vhidhiyo muUI yemutengi app, iyo mutengi app inotora iyo midhiya rekodhi kubva kudhatabhesi uchishandisa API. Iyi media rekodhi ine master playlist URL.
Vhidhiyo inoridza inotora master playlist kubva kuCDN uye inosarudza kuti ndeupi rukova rwekutamba zvichibva pazvinhu zvakaita sehukuru hwekuona, screen resolution, network mamiriro, nezvimwe. Inobva yatora playlist nezvikamu zvevhidhiyo kubva kuCDN yotanga kuridza vhidhiyo. .
Iyi mhinduro iri nyore kwazvo kuendesa uye haidi kugadzirisa. Nezve scalability yevashandisi vazhinji, zvakakosha kuti uzive kuti, nekusarudzika, MediaConvert mabasa anowedzerwa kune imwechete mutsara unokwanisa kugadzirisa zana-mazana maviri mavhidhiyo panguva imwe chete (zvichienderana nedunhu). Mitsetse yekuwedzera inogona kugadzirwa (kusvika gumi padunhu rega rega), uye zvinonyanya kukosha zvinogona kupihwa mabasa kana zvawedzerwa kumitsetse. Pane zvakare sarudzo yekukumbira quota kuwedzera kubva kuAWS.
Mukupedzisa, otomatiki shanduko yevhidhiyo uchishandisa makore-yakavakirwa masevhisi seAWS Elemental MediaConvert inzira inoshanda yekuendesa emhando yepamusoro yekufambisa zvirimo pamidziyo pasina mutoro wekugadzirisa zvivakwa zvakaomarara. Iyi nzira haingorerutsa iyo vhidhiyo encoding process asi zvakare inowedzera scalability, kuve nechokwadi chekuti chikuva chako chinogona kubata kudiwa kuri kukura.
Nekushandisa maturusi akadai seS3, Lambda mashandiro, uye CloudFront pamwe chete neMediaConvert, unogona kugadzira uye kuburitsa inoshanduka HLS hova, ichipa vashandisi ruzivo rwakanyanya rwekuona.