- try:
- p = subprocess.Popen(cmd)
- ret = p.wait()
- if ret != 0:
- print >>sys.stderr, cmd[0], "exited with error code:", ret
- return False
- else:
- return True
- except OSError, e:
- print >>sys.stderr, "Failed to run", cmd[0], e
- except KeyboardInterrupt:
- print "Cancelled", cmd
- try:
- p.terminate()
- p.wait()
- except KeyboardInterrupt:
- p.send_signal(signal.SIGKILL)
- p.wait()
- return False
-
-
-def convert_flv_mp4(orig_filename):
- basename = os.path.splitext(orig_filename)[0]
- flv_filename = basename + ".flv"
- mp4_filename = basename + ".mp4"
- if orig_filename != flv_filename:
- os.rename(orig_filename, flv_filename)
- print "Converting %s to mp4" % flv_filename
- cmd = [
- "ffmpeg",
- "-i", flv_filename,
- "-acodec", "copy",
- "-vcodec", "copy",
- mp4_filename,
- ]
- if not exec_subprocess(cmd):
- return
- try:
- flv_size = os.stat(flv_filename).st_size
- mp4_size = os.stat(mp4_filename).st_size
- if abs(flv_size - mp4_size) < 0.05 * flv_size:
- os.unlink(flv_filename)
- else:
- print >>sys.stderr, "The size of", mp4_filename, "is suspicious, did ffmpeg fail?"
- except Exception, e:
- print "Conversion failed", e
-
-def convert_filename(filename):
- if os.path.splitext(filename.lower())[1] in (".mp4", ".flv"):
- f = open(filename)
- fourcc = f.read(4)
- f.close()
- if fourcc == "FLV\x01":
- convert_flv_mp4(filename)
+ try:
+ p = subprocess.Popen(cmd)
+ ret = p.wait()
+ if ret != 0:
+ print >>sys.stderr, cmd[0], "exited with error code:", ret
+ return False
+ else:
+ return True
+ except OSError, e:
+ print >>sys.stderr, "Failed to run", cmd[0], e
+ except KeyboardInterrupt:
+ print "Cancelled", cmd
+ try:
+ p.terminate()
+ p.wait()
+ except KeyboardInterrupt:
+ p.send_signal(signal.SIGKILL)
+ p.wait()
+ return False
+
+
+def check_command_exists(cmd):
+ try:
+ subprocess.check_output(cmd)
+ return True
+ except Exception:
+ return False
+
+def generate_remux_cmd(infile, outfile):
+ if check_command_exists(["avconv", "--help"]):
+ return [
+ "avconv",
+ "-i", infile,
+ "-bsf:a", "aac_adtstoasc",
+ "-acodec", "copy",
+ "-vcodec", "copy",
+ outfile,
+ ]
+
+ if check_command_exists(["ffmpeg", "--help"]):
+ return [
+ "ffmpeg",
+ "-i", infile,
+ "-bsf:a", "aac_adtstoasc",
+ "-acodec", "copy",
+ "-vcodec", "copy",
+ outfile,
+ ]
+
+ raise Exception("You must install ffmpeg or libav-tools")
+
+def remux(infile, outfile):
+ print "Converting %s to mp4" % infile
+ cmd = generate_remux_cmd(infile, outfile)
+ if not exec_subprocess(cmd):
+ # failed, error has already been logged
+ return False
+ try:
+ flv_size = os.stat(infile).st_size
+ mp4_size = os.stat(outfile).st_size
+ if abs(flv_size - mp4_size) < 0.1 * flv_size:
+ os.unlink(infile)
+ return True
+ else:
+ print >>sys.stderr, "The size of", outfile, "is suspicious, did avconv fail?"
+ return False
+ except Exception, e:
+ print >>sys.stderr, "Conversion failed", e
+ return False
+
+def convert_to_mp4(filename):
+ with open(filename) as f:
+ fourcc = f.read(4)
+ basename, ext = os.path.splitext(filename)
+
+ if ext == ".mp4" and fourcc == "FLV\x01":
+ os.rename(filename, basename + ".flv")
+ ext = ".flv"
+ filename = basename + ext
+
+ if ext in (".flv", ".ts"):
+ filename_mp4 = basename + ".mp4"
+ return remux(filename, filename_mp4)
+
+ return ext == ".mp4"
+
+
+def download_hds(filename, video_url, pvswf=None):
+ filename = sanify_filename(filename)
+ video_url = video_url.replace("http://", "hds://")
+ print "Downloading: %s" % filename
+ cmd = [
+ "livestreamer",
+ "-o", filename,
+ "%s pvswf=%s" % (video_url, pvswf),
+ "best",
+ ]
+ if exec_subprocess(cmd):
+ return convert_to_mp4(filename)
+ else:
+ return False
+
+def download_hls(filename, video_url):
+ filename = sanify_filename(filename)
+ video_url = video_url.replace("http://", "hlsvariant://")
+ print "Downloading: %s" % filename
+ cmd = [
+ "livestreamer",
+ "-o", filename,
+ video_url,
+ "best",
+ ]
+ if exec_subprocess(cmd):
+ return convert_to_mp4(filename)
+ else:
+ return False