Can I read reddit video info using praw?

Viewed 283

I am learning to use PRAW to do configuration or read information on reddit through python.

Now I want to read the video contents online (size, duration, etc) (I uploaded one video from my account as a test video), which instance should I use and which method should I call?

I just read that PRAW could submit video, is there any method to read video details?

2 Answers

I think PRAW only let's you submit view with InlineVideo

you could try scraping it from the subreddit

import praw

r = praw.Reddit(<auth info here>)

sub = r.subreddit("some_sub")

posts = sub.hot(limit=100)

vids = []

for p in posts:
    try:
        url = p.media['reddit_media']['fallback_url']
        url = url.split("?")[0]
        name = p.title[:30].rstrip() + ".mp4"
        vids.append((url, name))
    except:
        pass

You can use ffmpeg/ffprobe Note, a lot of videos on imgur that show up on reddit have a .gifv extension when praw returns them. Just change the extension to .mp4.

import subprocess
vidurl = 'https://i.imgur.com/rEUoCkG.mp4'

This will give a lot of data in a list:

pcmd = 'ffprobe -v error -show_format -show_streams {}'.format(vidurl) 
data = subprocess.check_output(pcmd, shell=True).decode()
data.splitlines()



['[STREAM]',
 'index=0',
 'codec_name=h264',
 'codec_long_name=H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10',
 'profile=Constrained Baseline',
 'codec_type=video',
 'codec_tag_string=avc1',
 'codec_tag=0x31637661',
 'width=720',
 'height=720',
 'coded_width=720',
 'coded_height=720',
 'closed_captions=0',
 'has_b_frames=0',
 'sample_aspect_ratio=1:1',
 'display_aspect_ratio=1:1',
 'pix_fmt=yuv420p',
 'level=31',
 'color_range=unknown',
 'color_space=unknown',
 'color_transfer=unknown',
 'color_primaries=unknown',
 'chroma_location=left',
 'field_order=unknown',
 'refs=1',
 'is_avc=true',
 'nal_length_size=4',
 'id=N/A',
 'r_frame_rate=30/1',
 'avg_frame_rate=30/1',
 'time_base=1/15360',
 'start_pts=0',
 'start_time=0.000000',
 'duration_ts=501760',
 'duration=32.666667',
 'bit_rate=1945005',
 'max_bit_rate=N/A',
 'bits_per_raw_sample=8',
 'nb_frames=980',
 'nb_read_frames=N/A',
 'nb_read_packets=N/A',
 'DISPOSITION:default=1',
 'DISPOSITION:dub=0',
 'DISPOSITION:original=0',
 'DISPOSITION:comment=0',
 'DISPOSITION:lyrics=0',
 'DISPOSITION:karaoke=0',
 'DISPOSITION:forced=0',
 'DISPOSITION:hearing_impaired=0',
 'DISPOSITION:visual_impaired=0',
 'DISPOSITION:clean_effects=0',
 'DISPOSITION:attached_pic=0',
 'DISPOSITION:timed_thumbnails=0',
 'TAG:language=eng',
 'TAG:handler_name=VideoHandler',
 'TAG:vendor_id=[0][0][0][0]',
 '[/STREAM]',
 '[FORMAT]',
 'filename=https://i.imgur.com/rEUoCkG.mp4',
 'nb_streams=1',
 'nb_programs=0',
 'format_name=mov,mp4,m4a,3gp,3g2,mj2',
 'format_long_name=QuickTime / MOV',
 'start_time=0.000000',
 'duration=32.667000',
 'size=7947044',
 'bit_rate=1946194',
 'probe_score=100',
 'TAG:major_brand=isom',
 'TAG:minor_version=512',
 'TAG:compatible_brands=isomiso2avc1mp41',
 "TAG:title=''",
 'TAG:encoder=Lavf58.12.100',
 "TAG:comment=''",
 '[/FORMAT]']

This will pare it down to selected, if available:

pcmd = 'ffprobe -v error -select_streams v:0 -show_entries stream=avg_frame_rate,size,width,height,duration,bit_rate -of default=noprint_wrappers=1 {}'.format(vidurl)


['width=720',
 'height=720',
 'avg_frame_rate=30/1',
 'duration=32.666667',
 'bit_rate=1945005']
Related