Created
January 30, 2013 21:28
-
-
Save agscala/4677188 to your computer and use it in GitHub Desktop.
python vine2gif.py [vine_url] [output_file] Scrapes vine url, downloads video, and converts the file to a gif with mplayer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pyquery import PyQuery as pq | |
import sys | |
import subprocess | |
import os | |
from urllib2 import urlopen, URLError, HTTPError | |
def convert(input_file, output_gif): | |
print "CONVERT", input_file, " => ", output_gif | |
subprocess.call(["mplayer", input_file, "-ao", "null", "-ss", "0:0:0", "-endpos", "6", "-vo", "gif89a:fps=8:output=%s" % output_gif, "-vf", "scale=240:180"]) | |
def dlfile(url): | |
# Open the url | |
try: | |
f = urlopen(url) | |
print "downloading " + url | |
# Open our local file for writing | |
output_file = os.path.basename(url) | |
with open(output_file, "wb") as local_file: | |
local_file.write(f.read()) | |
return output_file | |
#handle errors | |
except HTTPError, e: | |
print "HTTP Error:", e.code, url | |
except URLError, e: | |
print "URL Error:", e.reason, url | |
if __name__ == "__main__": | |
try: | |
url = sys.argv[1] | |
output = sys.argv[2] | |
except IndexError: | |
sys.exit(1) | |
print "Creating [%s] from [%s]" % (url, output) | |
d = pq(url=url) | |
video_url = d("video#post source").attr("src") | |
video_url = video_url.partition("?")[0] | |
filename = dlfile(video_url) | |
convert(filename, output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 42 doesn't work anymore, it should be this instead: