Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9b16463

Browse files
Merge pull request #653 from Akash20x/main
Reddit Video Downloader
2 parents 3980e83 + ca993ea commit 9b16463

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed
15.4 KB
Loading[フレーム]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Reddit Video Downloader
2+
3+
### Description:
4+
5+
6+
This is a python script which will download the video of the post in reddit by asking user url of page of the post and also display post title,
7+
subreddit and author name on the console. The video is stored in our local system as download.mp4
8+
### Libraries used:
9+
10+
- requests
11+
12+
### Aim:
13+
14+
To download videos from the reddit posts.
15+
16+
### Purpose:
17+
18+
In reddit, there is no option to download videos which people
19+
post on their account posts. So this script helps to download those videos.
20+
We just want to copy the url of the video on reddit. .
21+
22+
### Workflow:
23+
24+
Import json and get & except module from requests libraries in the Python file that you are going to use to get the reddit post information and video.
25+
Using get we can request data from the url and extract its json data and except is used to provide exceptions.
26+
Now we can easily get the information and links about the post and the video from that web page using json data and display it in our console.
27+
Then we will create a file download.mp4 and store the video in this file.
28+
29+
### Setup Instructions:
30+
31+
1. Install the requests.
32+
2. Run the program
33+
3. The output is displayed in the terminal.
34+
35+
### Output:
36+
37+
<img src='Images/output.png'>
38+
39+
### Author:
40+
41+
Akash Jain
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from requests import get, exceptions
2+
import json
3+
4+
def get_user_agent():
5+
return 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.20 (KHTML, like Gecko) Chrome/11.0.669.0 Safari/534.20'
6+
7+
def get_video(url):
8+
try: # checks if link is valid
9+
r = get(
10+
url + '.json',
11+
headers = {'User-agent': get_user_agent()}
12+
)
13+
except exceptions.MissingSchema:
14+
print('Please provide a valid URL', 'error')
15+
quit()
16+
17+
if 'error' in r.text:
18+
if r.status_code == 404:
19+
print('Post not found', 'error')
20+
21+
quit()
22+
23+
try:
24+
json_data = json.loads(r.text)[0]['data']['children'][0]['data']
25+
print('Post Found!')
26+
print(f'Title: {json_data["title"]}')
27+
print(f'In sub-reddit: {json_data["subreddit_name_prefixed"]}')
28+
print(f'Posted by: {json_data["author"]}')
29+
print('Video is successfully downloaded')
30+
except:
31+
print('Post not found')
32+
quit()
33+
34+
try: # checks if post contains video
35+
video_url = json_data['secure_media']['reddit_video']['fallback_url']
36+
r = get(video_url).content
37+
with open('download.mp4', 'wb') as file:
38+
file.write(r)
39+
except TypeError:
40+
print('Only posts with videos are supported')
41+
42+
url=input('Enter Video Post Url:')
43+
get_video(url)
44+
45+
#Example:
46+
#take url='https://www.reddit.com/r/IndianGaming/comments/odcp6v/240hz_vs_60hz_gaming_monitor/'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requests

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /