Embed reddit video into a html page

Reddit API

Is there any possible way to embed a Reddit video post into a HTML page with the API? I'm working on a website that gets random posts from a users inputed subreddit, but currently it can only display image posts, I was wondering if I could be able to embed video posts aswell

Posted Sep 2, 2023, 6:04 PM UTC

16
notifications_appAlerts for Reddit Developer•

Can you share what you tried? For example, what is a sample video URL you tried to display, how are you trying to embed it, and what is the current result?

1
noodguycoderman223OP•

I tried getting the permalink and setting the <video> tags source as the permalink but it didnt show any video, just the controls

1
notifications_appAlerts for Reddit Developer•

The permalink to the whole post? Yeah, that won't do it - the whole post isn't a video.

Looks like the media_embed content is what you're looking for?

1
noodguycoderman223OP•

Ohhh kk

1
noodguycoderman223OP•

I tried this:

fetch(`https://www.reddit.com/r/videos/random.json`)
      .then(response => response.json())
      .then(data => {
        const post = data[0].data.children[0].data;
        const imageUrl = post.media_embed;
        if (imageUrl) {
document.getElementById('img').innerHTML = `<video width="320" height="240" controls><source src="${imageUrl}"></video>`
}
})

Still giving the same result

1
RaiderBDevphoton-reddit.com Developer•

You have to actually take a look at the json data you are receiving. Some posts use iframes, others are gifs, and others are videos with a separate audio track.

There are a lot of inconsistencies and things won't "just" work.

1