Upgrade to Pro — share decks privately, control downloads, hide ads and more …

AWSで作る動画自動変換と配信機構

 AWSで作る動画自動変換と配信機構

2021-04-27(Tue) 19:00〜
Fusic Tech Live Vol.1:AWS 活用事例 にて発表
https://fusic.connpass.com/event/206957/

withelmo

April 27, 2021
Tweet

Other Decks in Programming

Transcript

  1. ࣗݾ঺հ ਿຊ ৻ଠ࿠ Sugimoto Shintaro ‣ I D - @withelmo

    @withelmo
 ‣ ࣗݾ঺հ • Fusic ઌਐٕज़෦໳/෦໳௕/γχΞɾϓϦϯγύϧΤϯδχΞ • ػցֶशνʔϜͱIoTνʔϜͱΤϯδχΞϦϯάͯ͠·͢ ‣ ޷͖ͳAWSαʔϏε • S 3 ‣ Skill s 2 image
  2. 01. ࠓճ࠾༻ͨ͠ΞʔΩςΫνϟ 19 2. ಈըม׵ॲཧͷ։࢝ • S3ͷPUTΠϕϯτͰLambda͕
 τϦΨʔ • ҎԼΛࢦఆͯ͠Lambda͔Β

    MediaConvertͷJobΛ࡞੒ 1. MediaConvert Job Templat e 2. ೖྗϑΝΠϧͷS3Ωʔ 3. ग़ྗઌͷS3ϑΥϧμʔ 4. MediaConvert Queu e 5. IAM Role
  3. 1-1. AWS Elemental MediaConvert ઃఆ߲໨͸๲େ … ͚ͩͲ΄ͱΜͲσϑΥϧτͰྑ͍ײ͡ʹͳΓ·͢ʢͨͿΜʣ ࠓճJob template࡞੒Ͱೖྗͨ͠ͷ͸ҎԼ5ͭͷ߲໨ͷΈ •

    ςϯϓϨʔτͷ໊শ • ೖྗͷ௥Ճ • ग़ྗάϧʔϓͷ௥ՃʢετϦʔϛϯάύοέʔδλΠϓͷબ୒ʣ • ग़ྗϑΝΠϧͷ໊લम০ࢠͷઃఆ • ग़ྗϏσΦͷϏοτϨʔτ 26 { "Name": “mc-template-name", "Settings": { "TimecodeConfig": { "Source": "ZEROBASED" }, "OutputGroups": [ { "Name": "Apple HLS", "Outputs": [ { "ContainerSettings": { "Container": "M3U8", "M3u8Settings": { "AudioFramesPerPes": 4, "PcrControl": "PCR_EVERY_PES_PACKET", "PmtPid": 480, "PrivateMetadataPid": 503, "ProgramNumber": 1, "PatInterval": 0, "PmtInterval": 0, "Scte35Source": "NONE", "NielsenId3": "NONE", "TimedMetadata": "NONE", "VideoPid": 481, "AudioPids": [ 482, 483, 484, 485, 486, 487, 488, 489, 490, 491, 492 ], "AudioDuration": "DEFAULT_CODEC_DURATION" } }, "VideoDescription": { "ScalingBehavior": "DEFAULT", "TimecodeInsertion": "DISABLED", "AntiAlias": "ENABLED", "Sharpness": 50, "CodecSettings": { "Codec": "H_264", "H264Settings": { "InterlaceMode": "PROGRESSIVE", "ScanTypeConversionMode": "INTERLACED", "NumberReferenceFrames": 3, "Syntax": "DEFAULT", "Softness": 0, "GopClosedCadence": 1, "GopSize": 90, "Slices": 1, "GopBReference": "DISABLED", "MaxBitrate": 1000000, "SlowPal": "DISABLED", "EntropyEncoding": "CABAC", "FramerateControl": "INITIALIZE_FROM_SOURCE", "RateControlMode": "QVBR", "QvbrSettings": { "QvbrQualityLevel": 7,
  4. 1-1. AWS Elemental Media Convert • ग़ྗάϧʔϓͷ௥ՃʢετϦʔϛϯάύοέʔδλΠϓͷબ୒ʣ • HLSΛબ୒ʢH.264/ACC͕σϑΥϧτબ୒ʣ •

    ಈը࠶ੜଆͷཁ݅ʹ߹Θͤͯબ୒ɺௐ੔͕ඞཁ • ࠓճ͸ϒϥ΢βͰͷಈը࠶ੜ͕ཁ݅ͰɺϓϨʔϠʔ͸react-playerͰ࣮૷ • ͜͜΋ϋϚΓͲ͜ΖຬࡌͰ͕͢ɺࠓճ͸ׂѪ • ࠓޙͷFusic Tech LiveͰReactձ͕͋Ε͹ൃද͋Δ͔΋!? 27
  5. 1-1. AWS Elemental Media Convert • ग़ྗϑΝΠϧͷ໊લम০ࢠͷઃఆ • ೖྗͱಉҰՕॴʹग़ྗ͢Δ͜ͱΛ૝ఆͷ޻෉ʢ͔ͳʁͱࢥͬͯΔ )

    • ग़ྗϑΝΠϧ໊ͷҰ෦ͱͯ͠෇༩ͯ͠ग़ྗ͞ΕΔ • ҎԼͷਤ͸ɺೖྗϑΝΠϧmovie.mp4ͷͱ͖ͷग़ྗྫ 28
  6. 1-1. AWS Elemental Media Convert • ग़ྗϏσΦͷϏοτϨʔτ • Ϩʔτ੍ޚϞʔυ͸QVBRʢࣗಈͰྑ͍ײ͡ʹͯ͘͠ΕΔʣ •

    ࠷େϏοτϨʔτ͸1Mbpsɺ඼࣭Ϩϕϧ7(σϑΥϧτ ) • ม׵ͷ࣮੷ྫ • ݩಈըɿϑΝΠϧαΠζ3.8GB (mp4, 1920x1080, Ϩʔτ10Mbps, 50෼ ) • ม׵ޙɿϑΝΠϧαΠζ213.2MB 29
  7. 1-2. Amazon EventBridge • Πϕϯτόε • ΞΫηεͰ͖ΔϢʔβʔΛఆٛ͠ɺΠϕϯτͷΞΫηείϯτϩʔϧΛ͕ॊೈʹՄೳ • σϑΥϧτόε͸ॴ༗ऀͷΈ͕࢖༻Մೳ •

    ϧʔϧ • ύλʔϯఆٛ • CloudWatchEventsͱ΄΅ಉ͡ • εέδϡʔϧ࣮ߦʹ΋ରԠ • λʔήοτબ୒ • ͞·͟·ͳAWSαʔϏεʹରԠ • ࠓճ͸Lambdaؔ਺Λઃఆ 31
  8. 1-3. Amazon CloudFront • ॺ໊෇͖Cookie • Ҏલ͸AWSΞΧ΢ϯτϧʔτϢʔβʔ͕ൃߦ͢ΔΩʔͰͷॺ໊ͷΈͰ͕ͨ͠ɺ 
 ͍·͸Ϣʔβʔ͕ੜ੒ͨ͠ΩʔϖΞΛ࢖༻͢Δ͜ͱ͕Մೳɺ͔ͭਪ঑ •

    ॺ໊෇͖Cookieͷੜ੒ʹ͸ҎԼ͕ඞཁ • ൿີ伴 • CloudFront Public Key ID • ΧελϜϙϦγʔ • ॺ໊෇͖Cookieͷੜ੒͸AWS SDKͰՄೳ • ྫʣRubyͰ͸Aws::CloudFront::CookieSigner#signed_cookie • https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/CloudFront/CookieSigner.html 34
  9. 1-3. Amazon CloudFront [Tips] • CloudFrontͰͷಈ࡞֬ೝ͸͘͢͝೉͍͠ • ઃఆ൓өʹ͕͔͔࣌ؒΔ • ґଘ͍ͯ͠ΔαʔϏε͕ଟذʹΘͨΔͨΊɺ໰୊Օॴͷಛఆͮ͠Β͍

    • ΫϥΠΞϯτͷΩϟογϡ͕ޮ͍͍ͯΔ͜ͱ΋͋Δ • γϯϓϧͳઃఆ͔Β࢝ΊɺҰͭͣͭઃఆΛ૿΍͠ɺ֬ೝ͠ͳ͕ΒਐΊΔͷ͕ྑ͍Ͱ͢ 35
  10. 1-3. Amazon CloudFront [Tips] • ࠓճͷྫʢͬ͘͟ΓͰ͕͢ʣ • ॺ໊෇͖Cookie੍ݶͳ͠ͰCloudFront(+S3)Λߏங • ίϯςϯπͷμ΢ϯϩʔυΛcurlίϚϯυͰ֬ೝ

    • ಈըϓϨʔϠʔͰHLS͕࠶ੜՄೳ͔֬ೝ • ॺ໊෇͖CookieͷઃఆΛͯ͠ɺcurlίϚϯυͰμ΢ϯϩʔυΛ֬ೝ • ࠷ޙʹಈըϓϨʔϠʔͰ࠶ੜՄೳ͔֬ೝ 36
  11. 2. ΞʔΩςΫνϟͰ໎ͬͨͱ͜Ζ 40 • ಈըม׵εςʔλεͷߋ৽ॲཧ • ࠓճ࠾༻ͨ͠ͷ͸ɺ • MediaConverterͷ׬ྃΠϕϯτΛड͚औͬͯɺ •

    Lambda͕S3ʹϑΝΠϧΛPUT • ΞϓϦͷఆظॲཧͰϑΝΠϧνΣοΫ͠ɺ 
 σʔλϕʔεΛߋ৽ ಈըม׵׬ྃ ϑΝΠϧ(JSON)
  12. 2. ΞʔΩςΫνϟͰ໎ͬͨͱ͜Ζ 45 S3 SQS ALB RDS Proxy ෛՙ଱ੑ ʢಈըม׵ͷස౓ʣ

    ◦ ◦ ˚ ✕ ϦΞϧλΠϜੑ ✕ ✕ ◦ ◦ ࣮૷ίετ ◦ ◦ ✕ ✕ ӡ༻ίετ ◦ ˚ ✕ ✕
  13. 3-1. Infrastructure as Codeͱͯ͠΍ͬͨ͜ͱ 50 • ݁࿦ • ಈըม׵ॲཧपΓ͸SAMͰ؅ཧ •

    S3͸TerraformͰ؅ཧ • ཧ༝ • S3͸LambdaΑΓ΋ΞϓϦؔ࿈Ϧιʔεͱͷ݁߹౓͕ߴ͔ͬͨ • ྫʣEC2 Role, CloudFront(Origin, CORSͳͲ)
  14. 3-2. Infrastructure as CodeͰ͖ͳ͔ͬͨ͜ͱ 56 » terraform -version Terraform v0.14.6

    + provider registry.terraform.io/hashicorp/aws v3.35.0 + provider registry.terraform.io/hashicorp/template v2.2.0
  15. 3-2. Infrastructure as CodeͰ͖ͳ͔ͬͨ͜ͱΛ΍ͬͯΈͨʂ 58 • CloudFront༻Ωʔάϧʔϓͷ࡞੒ 
 
 


    
 
 • CloudFrontͷॺ໊෇͖CookieͰͷΞΫηε੍ݶઃఆ(Restrict Viewer Access/Trusted Key Groups) resource aws_cloudfront_public_key cf_public_key { name = "movie-public-key " comment = "CloudFront public key " encoded_key = file("./cloudfront/public_key.pem" ) } resource aws_cloudfront_key_group cf_key_group { name = "movie-key-group " items = [aws_cloudfront_public_key.cf_public_key.id ] comment = “movie-key-group " } resource aws_cloudfront_distribution movie-cf { default_cache_behavior { trusted_key_groups = [aws_cloudfront_key_group.cf_key_group.id ] } }