Skip to content

Instantly share code, notes, and snippets.

@Schnouki
Created May 29, 2009 12:30
Show Gist options
  • Save Schnouki/119928 to your computer and use it in GitHub Desktop.
Save Schnouki/119928 to your computer and use it in GitHub Desktop.
Python script to download podcast from TED and convert videos to audio-only .ogg files
#! /usr/bin/env python
#
# Copyright (c) 2009, Thomas Jost <thomas.jost@gmail.com>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
import feedparser
import os
import os.path
import subprocess
import sys
PODCAST_URL = "http://feeds.feedburner.com/tedtalks_video"
DUMP_FILE = 'audiodump.wav'
nb_to_fetch = 5
if len(sys.argv) == 2:
nb_to_fetch = int(sys.argv[1])
print "Fetching %d items" % nb_to_fetch
d = feedparser.parse(PODCAST_URL)
fetched = 0
for e in d.entries:
fetched += 1
if fetched > nb_to_fetch: break
title = e.subtitle
author = e.author
url = e.id
dst_file = os.path.splitext(os.path.basename(url))[0] + '.ogg'
print '%s: "%s"' % (author, title)
if os.path.exists(dst_file):
print "\t* Already downloaded"
else:
print "\t* Downloading..."
if not os.path.exists(DUMP_FILE):
os.mkfifo(DUMP_FILE, 0600)
dl_cmd = ['mplayer', '-nolirc', '-really-quiet', '-vo', 'null', '-ao', 'pcm:fast', url]
conv_cmd = ['oggenc', '-Q', '-q3', '-l', 'TEDTalks', '-a', author, '-t', title, '-o', dst_file, DUMP_FILE]
conv_p = subprocess.Popen(conv_cmd)
dl_p = subprocess.Popen(dl_cmd)
retcode = dl_p.wait()
if retcode != 0:
print "\t! Error: mplayer exited with code %d" % retcode
break
retcode = conv_p.wait()
if retcode != 0:
print "\t! Error: oggenc exited with code %d" % retcode
break
print "\t* File ready: %s" % dst_file
if os.path.exists(DUMP_FILE):
os.remove(DUMP_FILE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment