<video style="outline:none;width:100%;background-color:#000" playsinline poster="<%= thumbnail %>"
    id="player" class="on-video_player video-js player-style-<%= params.player_style %>"
    <% if params.autoplay %>autoplay<% end %>
    <% if params.video_loop %>loop<% end %>
    <% if params.controls %>controls<% end %>>
    <% if (hlsvp = video.hls_manifest_url) && !CONFIG.disabled?("livestreams") %>
        <source src="<%= URI.parse(hlsvp).request_target %><% if params.local %>?local=true<% end %>" type="application/x-mpegURL" label="livestream">
    <% else %>
        <% if params.listen %>
            <% audio_streams.each_with_index do |fmt, i| %>
                <source src="/latest_version?id=<%= video.id %>&itag=<%= fmt["itag"] %><% if params.local %>&local=true<% end %>" type='<%= fmt["mimeType"] %>' label="<%= fmt["bitrate"] %>k" selected="<%= i == 0 ? true : false %>">
            <% end %>
        <% else %>
            <% if params.quality == "dash" %>
                <source src="/api/manifest/dash/id/<%= video.id %>?local=true&unique_res=1" type='application/dash+xml' label="dash">
            <% end %>

            <%
            fmt_stream.reject! { |f| f["itag"] == 17 }
            fmt_stream.sort_by! {|f| params.quality == f["quality"] ? 0 : 1 }
            fmt_stream.each_with_index do |fmt, i|
                src_url  = "/latest_version?id=#{video.id}&itag=#{fmt["itag"]}"
                src_url += "&local=true" if params.local

                quality = fmt["quality"]
                mimetype = HTML.escape(fmt["mimeType"].as_s)

                selected = params.quality ? (params.quality == quality) : (i == 0)
            %>
                <source src="<%= src_url %>" type="<%= mimetype %>" label="<%= quality %>" selected="<%= selected %>">
            <% end %>
        <% end %>

        <% preferred_captions.each do |caption| %>
            <track kind="captions" src="/api/v1/captions/<%= video.id %>?label=<%= caption.name %>&hl=<%= env.get("preferences").as(Preferences).locale %>"
                label="<%= caption.name %>">
        <% end %>

        <% captions.each do |caption| %>
            <track kind="captions" src="/api/v1/captions/<%= video.id %>?label=<%= caption.name %>&hl=<%= env.get("preferences").as(Preferences).locale %>"
                label="<%= caption.name %>">
        <% end %>
    <% end %>
</video>

<script id="player_data" type="application/json">
<%=
{
    "aspect_ratio" => aspect_ratio,
    "title" => video.title,
    "description" => HTML.escape(video.short_description),
    "thumbnail" => thumbnail,
    "preferred_caption_found" => !preferred_captions.empty?
}.to_pretty_json
%>
</script>
<script src="/js/player.js?v=<%= ASSET_COMMIT %>"></script>
