title: MPEG-Dash with secure S3 URLs
body: <p>I'm trying to set up a video server with MPEG-Dash where the video content is hosted on an S3 bucket. When hosted locally (nginx server) it works fine, but when I alter the MPD file to point to the S3 bucket, it doesn't work.</p>
  
  <p>Here's what I'm doing:</p>
  
  <p>--First I convert the video to multi-bitrate MP4</p>
  
  <pre><code>/usr/bin/ffmpeg -y -async 1 -vsync -1 -analyzeduration 999999999 -i &lt;SOURCE&gt; \
      -movflags faststart -keyint_min 30 -x264opts "keyint=30:min-keyint=30:no-scenecut" -g 30 -filter:v "scale=iw*min(320/iw\,180/ih):ih*min(320/iw\,180/ih), pad=320:180:(320-iw*min(320/iw\,180/ih))/2:(180-ih*min(320/iw\,180/ih))/2" -r:v 30 -s 320x180 -b:v 320k -c:v libx264 -pix_fmt yuv420p -profile:v baseline -level 3.0 -an "'.$base.'180p.mp4" \
      -movflags faststart -keyint_min 30 -x264opts "keyint=30:min-keyint=30:no-scenecut" -g 30 -filter:v "scale=iw*min(640/iw\,360/ih):ih*min(640/iw\,360/ih), pad=640:360:(640-iw*min(640/iw\,360/ih))/2:(360-ih*min(640/iw\,360/ih))/2" -r:v 30 -s 640x360 -b:v 640k -c:v libx264 -pix_fmt yuv420p -profile:v baseline -level 3.0 -an "'.$base.'360p.mp4" \
      -movflags faststart -keyint_min 30 -x264opts "keyint=30:min-keyint=30:no-scenecut" -g 30 -filter:v "scale=iw*min(1280/iw\,720/ih):ih*min(1280/iw\,720/ih), pad=1280:720:(1280-iw*min(1280/iw\,720/ih))/2:(720-ih*min(1280/iw\,720/ih))/2" -r:v 30 -s 1280x720 -b:v 1280k -c:v libx264 -pix_fmt yuv420p -profile:v baseline -level 3.1 -an "'.$base.'720p.mp4" \
      -c:v copy -c:a libfdk_aac -ac 2 -ar 48000 -b:a 96k "'.$base.'aacp.mp4"
  </code></pre>
  
  <p>--Then I dash it with MP4Box</p>
  
  <pre><code>/usr/bin/MP4Box -dash 10000 -rap -profile dashavc264:onDemand -out "stream.mpd" \
      180p.mp4#video \
      360p.mp4#video \
      720p.mp4#video \
      aacp.mp4#audio
  </code></pre>
  
  <p>--That results in the following MPD</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;!-- MPD file Generated with GPAC version 0.5.1-DEV-rev4065  on 2015-08-08T02:18:28Z--&gt;
  &lt;MPD xmlns="urn:mpeg:dash:schema:mpd:2011" minBufferTime="PT1.500000S" type="static" mediaPresentationDuration="PT0H1M56.11S" profiles="urn:mpeg:dash:profile:isoff-on-demand:2011, http://dashif.org/guildelines/dash264"&gt;
   &lt;ProgramInformation moreInformationURL="http://gpac.sourceforge.net"&gt;
    &lt;Title&gt;/webdata/temp/10/dash/10.mpd generated by GPAC&lt;/Title&gt;
   &lt;/ProgramInformation&gt;
  
   &lt;Period duration="PT0H1M56.11S"&gt;
    &lt;AdaptationSet segmentAlignment="true" maxWidth="1280" maxHeight="720" maxFrameRate="30" par="16:9" lang="und"&gt;
     &lt;Representation id="1" mimeType="video/mp4" codecs="avc1.42c01e" width="320" height="180" frameRate="30" sar="1:1" startWithSAP="1" bandwidth="349890"&gt;
      &lt;BaseURL&gt;180p_track1_dashinit.mp4&lt;/BaseURL&gt;
      &lt;SegmentBase indexRangeExact="true" indexRange="900-1075"&gt;
        &lt;Initialization range="0-899"/&gt;
      &lt;/SegmentBase&gt;
     &lt;/Representation&gt;
     &lt;Representation id="2" mimeType="video/mp4" codecs="avc1.42c01e" width="640" height="360" frameRate="30" sar="1:1" startWithSAP="1" bandwidth="701122"&gt;
      &lt;BaseURL&gt;360p_track1_dashinit.mp4&lt;/BaseURL&gt;
      &lt;SegmentBase indexRangeExact="true" indexRange="900-1075"&gt;
        &lt;Initialization range="0-899"/&gt;
      &lt;/SegmentBase&gt;
     &lt;/Representation&gt;
     &lt;Representation id="3" mimeType="video/mp4" codecs="avc1.42c01f" width="1280" height="720" frameRate="30" sar="1:1" startWithSAP="1" bandwidth="1401368"&gt;
      &lt;BaseURL&gt;720p_track1_dashinit.mp4&lt;/BaseURL&gt;
      &lt;SegmentBase indexRangeExact="true" indexRange="900-1075"&gt;
        &lt;Initialization range="0-899"/&gt;
      &lt;/SegmentBase&gt;
     &lt;/Representation&gt;
    &lt;/AdaptationSet&gt;
    &lt;AdaptationSet segmentAlignment="true" lang="und"&gt;
     &lt;Representation id="4" mimeType="audio/mp4" codecs="mp4a.40.2" audioSamplingRate="48000" startWithSAP="1" bandwidth="97736"&gt;
      &lt;AudioChannelConfiguration schemeIdUri="urn:mpeg:dash:23003:3:audio_channel_configuration:2011" value="2"/&gt;
      &lt;BaseURL&gt;aacp_track2_dashinit.mp4&lt;/BaseURL&gt;
      &lt;SegmentBase indexRangeExact="true" indexRange="837-1012"&gt;
        &lt;Initialization range="0-836"/&gt;
      &lt;/SegmentBase&gt;
     &lt;/Representation&gt;
    &lt;/AdaptationSet&gt;
   &lt;/Period&gt;
  &lt;/MPD&gt;
  </code></pre>
  
  <p>When I edit the MPD to have dynamic URLS:</p>
  
  <p>--PHP source --</p>
  
  <pre><code>&lt;?php
      $temp = S3URL("&lt;VIDEOBUCKET&gt;","10/dash/180p_track1_dashinit.mp4","653");
      echo "&lt;BaseURL&gt;".str_replace("https://&lt;VIDEOBUCKET&gt;.s3.amazonaws.com/10/dash/","",$temp)."&lt;/BaseURL&gt;".PHP_EOL;
  ?&gt;
  </code></pre>
  
  <p>--ECHOED DATA--</p>
  
  <pre><code>&lt;BaseURL&gt;180p_track1_dashinit.mp4?AWSAccessKeyId=XXXXXXXXXX&amp;Expires=XXXXXXXXXX&amp;Signature=XXXXXXXXXX&lt;/BaseURL&gt;
  </code></pre>
  
  <p>I get invalid content on the MPD file. I've looked around and tried moving the BaseURL to the Adaptation, Representation and other stanzas of the XML but nothing seems to work.</p>
  
  <p>What is the correct way of doing this?</p>