• Login
  • Register
  • Login Register
    Login
    Username/Email:
    Password:
    Or login with a social network below
  • Forum
  • Website
  • GitHub
  • Status
  • Translation
  • Features
  • Team
  • Rules
  • Help
  • Feeds
User Links
  • Login
  • Register
  • Login Register
    Login
    Username/Email:
    Password:
    Or login with a social network below

    Useful Links Forum Website GitHub Status Translation Features Team Rules Help Feeds
    Jellyfin Forum Support Troubleshooting Networking & Access Cloudflare Caching Rule and Worker for Jellyfin Video Transcoding – Configuration Rev

     
    • 0 Vote(s) - 0 Average

    Cloudflare Caching Rule and Worker for Jellyfin Video Transcoding – Configuration Rev

    Aksi
    Offline

    Junior Member

    Posts: 2
    Threads: 1
    Joined: 2024 Oct
    Reputation: 0
    Country:Ukraine
    #1
    2024-10-11, 11:49 AM
    Hello everyone,

    I'm setting up a caching solution for Jellyfin video transcoding using Cloudflare, and I've come across some questions regarding the best practices for caching video transcoding with Cloudflare Workers. I noticed that the caching documentation for Jellyfin seems to have been removed or changed on the official site, so I’m seeking advice from the community.

    I’ve configured a Cloudflare Worker script to handle video caching for Jellyfin, specifically targeting requests to /videos/. The script normalizes the URL by removing various query parameters (e.g., api_key, DeviceId, PlaySessionId, etc.), and then attempts to fetch the requested resource from the cache. If not found, it fetches it from the origin server and caches the response.

    Here is the Cloudflare Worker code I’m using:
    Code:
    addEventListener('fetch', (event) => {
      event.respondWith(handleRequest(event))
    })

    async function handleRequest(event) {
      const request = event.request
      const url = new URL(request.url)

      if (url.pathname.startsWith('/videos/')) {
        const paramsToRemove = [
          'api_key',
          'DeviceId',
          'PlaySessionId',
          // 'MediaSourceId',
          // 'VideoCodec',
          // 'AudioCodec',
          // 'AudioStreamIndex',
          // 'VideoBitrate',
          // 'AudioBitrate',
          // 'AudioSampleRate',
          // 'MaxFramerate',
          // 'TranscodingMaxAudioChannels',
          // 'RequireAvc',
          // 'Tag',
          // 'SegmentContainer',
          // 'MinSegments',
          // 'BreakOnNonKeyFrames',
          // 'h264-level',
          // 'h264-videobitdepth',
          // 'h264-profile',
          // 'h264-audiochannels',
          // 'aac-profile',
          // 'av1-profile',
          // 'av1-rangetype',
          // 'av1-level',
          // 'hevc-profile',
          // 'hevc-rangetype',
          // 'hevc-level',
          // 'hevc-deinterlace',
          // 'h264-rangetype',
          // 'h264-deinterlace',
          // 'TranscodeReasons',
          // 'runtimeTicks',
          // 'actualSegmentLengthTicks'
        ]

        for (const param of paramsToRemove) {
          url.searchParams.delete(param)
        }

        const normalizedUrl = url.toString()

        const cacheRequest = new Request(normalizedUrl, {
          method: request.method,
          headers: request.headers,
        })

        const cache = caches.default

        let response = await cache.match(cacheRequest)

        if (!response) {
          response = await fetch(request)

          if (response.ok) {
            const responseClone = response.clone()

            event.waitUntil(
              cache.put(
                cacheRequest,
                responseClone
              )
            )
          }
        }

        return response
      }

      return fetch(request)
    }

    Example Request 
    Here's an example of a request that would be processed: 
    Code:
    /videos/REDACTED/hls1/main/1.mp4?DeviceId=REDACTED&MediaSourceId=REDACTED&VideoCodec=av1,hevc,h264,hevc&AudioCodec=aac&AudioStreamIndex=2&VideoBitrate=59808000&AudioBitrate=192000&AudioSampleRate=44100&MaxFramerate=23.976025&PlaySessionId=REDACTED&api_key=REDACTED&TranscodingMaxAudioChannels=2&RequireAvc=false&Tag=REDACTED&SegmentContainer=mp4&MinSegments=2&BreakOnNonKeyFrames=True&hevc-level=153&hevc-videobitdepth=10&hevc-profile=main10&hevc-audiochannels=2&aac-profile=lc&av1-profile=main&av1-rangetype=SDR,HDR10,HLG&av1-level=19&hevc-rangetype=SDR,HDR10,HLG&hevc-deinterlace=true&h264-profile=high,main,baseline,constrainedbaseline,high10&h264-rangetype=SDR&h264-level=52&h264-deinterlace=true&TranscodeReasons=ContainerNotSupported,%REDACTED&runtimeTicks=75080000&actualSegmentLengthTicks=136800000

    Questions
    1. Is this configuration optimal for caching transcoded video streams from Jellyfin? Are there any parameters that should not be removed from the cache key?
    2. Are there any specific considerations or settings I should apply for Cloudflare's cache rules to avoid issues with video playback or session handling?
    3. Since the Jellyfin documentation regarding caching is no longer available, does anyone have updated best practices for caching video streams or transcoding results?
    TheDreadPirate
    Offline

    Community Moderator

    Posts: 15,374
    Threads: 10
    Joined: 2023 Jun
    Reputation: 460
    Country:United States
    #2
    2024-10-11, 07:45 PM (This post was last modified: 2024-10-11, 08:04 PM by TheDreadPirate. Edited 1 time in total.)
    Caching of video is not really suggested for Jellyfin, which was why we removed the documentation for it. There is only one situation I can think of to justify video caching.

    1) Your reverse proxy is located at a VPS/hosting service you rented/purchased
    2) Your actual jellyfin server is located somewhere else from the proxy (your home?)
    3) You use syncplay a lot with a lot of simultaneous watchers

    If all 3 are true, caching of video makes sense. And, in this setup, the caching would be on a host you likely own/control.

    HAVING SAID THAT, since you would be using CF's services, it is against Cloudflare's TOS to serve video on their free tier proxy/caching services. They have a separate service, NOT free, for serving/caching video.

    https://developers.cloudflare.com/suppor...loudflare/

    I know you are using CF Workers, but the same TOS applies.

    https://community.cloudflare.com/t/tos-f...ent/228259

    As such, we cannot assist with violating CF's TOS.

    https://forum.jellyfin.org/t-jellyfin-forum-rules

    Rule 2-7.

    Quote:Post information that may violate the ToS (Terms of Service) of other services; if a ToS is unclear, you should assume that it is against the ToS.
    Jellyfin 10.10.7 (Docker)
    Ubuntu 24.04.2 LTS w/HWE
    Intel i3 12100
    Intel Arc A380
    OS drive - SK Hynix P41 1TB
    Storage
        4x WD Red Pro 6TB CMR in RAIDZ1
    [Image: GitHub%20Sponsors-grey?logo=github]
    Aksi
    Offline

    Junior Member

    Posts: 2
    Threads: 1
    Joined: 2024 Oct
    Reputation: 0
    Country:Ukraine
    #3
    2024-10-12, 03:33 AM
    Thanks for the info! I'm only using a rented VPS and utilizing rclone from Google Drive. The caching is needed because sometimes rclone works slowly, and for 4K videos, the processing time can be long due to the weak CPU on my server.

    I understand now that caching may not be the best solution given the situation with Cloudflare’s TOS. I’ll keep it without caching for now but will adjust the transcoding Encoding preset to ultrafast, as it helps to play the video without throttling.

    Thanks again for the clarification!
    « Next Oldest | Next Newest »

    Users browsing this thread: 1 Guest(s)


    • View a Printable Version
    • Subscribe to this thread
    Forum Jump:

    Home · Team · Help · Contact
    © Designed by D&D - Powered by MyBB
    L


    Jellyfin

    The Free Software Media System

    Linear Mode
    Threaded Mode