Automated Video Extraction from Reddit with Apple Shortcuts


I have two kids with their own iPhones and iPads and all of their devices are locked down to prevent unrestricted web browsing. With these limits in place, its difficult for me to share funny or interesting videos I see on Reddit. Can I make a shortcut to find the source of those videos, convert them to a format that is easily shareable, and put them straight in the Messages app? Let’s try! tl;dr, I could.


Investigation


I started by using Safari’s Web Inspector and focusing on the section of the page that is responsible for playing the video. Right away, it looked like the HTML element named <shreddit-player> was going to be a promising starting point.



Before continuing, I decided to try a couple of quick things that might make finding a solution easier:


  1. Turning off JavaScript in the hope that the HTML returned might be simpler or use a standard <video> element to play the media. Instead, I got a non-functional page that showed only the first frame of the video and a loading spinner that animated indefinitely.

Back to the <shreddit-player> element. It has a couple of attributes that looked interesting:


  1. “src”: it’s a URL pointing to an .m3u8 file, which is a playlist. The query parameters are probably indexing into a specific resource in the playlist that specifies the video. Here’s an example.
  2. “packaged-media-json”: a JSON payload that contains links to individual .mp4 video files with a range of resolutions.

This is what a truncated payload looks like:


{
  "playbackMp4s": {
    "duration": 12,
    "permutations": [
      {
        "source": {
          "url": "https://packaged-media.redd.it/zw4c1qw9tj9g1/pb/m2-res_294p.mp4?m=DASHPlaylist.mpd&v=1&e=1766815200&s=87ba26df4695eb69922a17ecb1ac5a9b66d40874",
          "dimensions": {
            "width": 220,
            "height": 294
          }
        }
      },
      {
        "source": {
          "url": "https://packaged-media.redd.it/zw4c1qw9tj9g1/pb/m2-res_960p.mp4?m=DASHPlaylist.mpd&v=1&e=1766815200&s=72da95af9a4b2342ba9c73ed4c364b36653dd7f9",
          "dimensions": {
            "width": 720,
            "height": 960
          }
        }
      }
    ]
  }
}


Ultimately, I decided to use the JSON.


I took the “url” in the “source” object and, using curl, requested it. But it came back with a response of 403 Forbidden. That was surprising, until I noticed my obvious mistake: I forgot to decode the & in the URLs. After doing so, the URL gave me the video I was looking for!


After figuring out how to get the video, here’s the possible strategy I came up with for an end-to-end workflow for this shortcut:


  1. Go to the page with the video I want to share.
  2. Initiate the shortcut.
  3. Find the custom video player element from the DOM.
  4. Get the JSON payload from the element and parse that to get a URL to the video.
  5. Download it to my device.
  6. Possibly convert it to a GIF for easier sharing?
  7. Put it in a new message in the Messages app.

Implementation


I was able to get it working in 5 actions, with one of the actions (extracting the URL of the video) doing most of the work.



This lines up with the strategy above, with the exceptions of steps 3-4, which are combined into the “Run JavaScript” on Active Safari Tab” action below:


var result = "";

const players = document.querySelectorAll('shreddit-player');
if (players.length > 0) {
  // Assume the first player is the one we want.
  var json = players[0].getAttribute('packaged-media-json');
  if (json !== null && json.length > 0) {
    const obj = JSON.parse(json);
    // Assume the last permutation is the best.
    const last = obj.playbackMp4s.permutations.slice(-1)[0];

    result = last.source.url;
  }
}

completion(result);

This code is naive in that it makes multiple assumptions about the structure of the HTML, the JSON, and doesn’t handle an unparseable payload. This is a one-off shortcut for fun, so I’m skipping all that. It could always be made more robust in the future.


Results


It’s a little slow, and it requires changing Content & Privacy Restrictions to “Allow JavaScript from Apple Events”. But it does work.



Depending on the your platform, the second action is slightly different and may require tweaking the shortcut:


  1. On macOS: change to “Run JavaScript on Active Safari Tab”
  2. On iOS: change to “Run JavaScript on Shortcut Input”

And that’s it! This simple shortcut is available on GitHub.