]> code.delx.au - webdl/blobdiff - common.py
webdl: open files in binary mode for windows compatibility
[webdl] / common.py
index accd6dd506eab83f6e6d2acf14ca94241f000731..2a5e378ad54ed906afb19b3250862858ed22853d 100644 (file)
--- a/common.py
+++ b/common.py
@@ -96,7 +96,7 @@ def urlopen(url, max_age):
             return open(filename)
 
     src = _urlopen(url)
-    dst = open(filename, "w")
+    dst = open(filename, "wb")
     try:
         shutil.copyfileobj(src, dst)
     except Exception, e:
@@ -166,15 +166,39 @@ def exec_subprocess(cmd):
     return False
 
 
-def avconv_remux(infile, outfile):
+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 = [
-        "avconv",
-        "-i", infile,
-        "-acodec", "copy",
-        "-vcodec", "copy",
-        outfile,
-    ]
+    cmd = generate_remux_cmd(infile, outfile)
     if not exec_subprocess(cmd):
         # failed, error has already been logged
         return False
@@ -203,7 +227,7 @@ def convert_to_mp4(filename):
 
     if ext in (".flv", ".ts"):
         filename_mp4 = basename + ".mp4"
-        return avconv_remux(filename, filename_mp4)
+        return remux(filename, filename_mp4)
 
     return ext == ".mp4"
 
@@ -231,7 +255,7 @@ def download_urllib(filename, url, referrer=None):
     print "Downloading: %s" % filename
     try:
         src = _urlopen(url, referrer)
-        dst = open(filename, "w")
+        dst = open(filename, "wb")
         while True:
             buf = src.read(1024*1024)
             if not buf:
@@ -316,16 +340,17 @@ def download_hls_fetch_segment(outf, segment_url):
             pass
 
 def download_hls(filename, m3u8_master_url, hack_url_func=None):
+    filename = sanify_filename(filename)
+    print "Downloading: %s" % filename
+
     if hack_url_func is None:
         hack_url_func = lambda url: url
 
     tmpdir = tempfile.mkdtemp(prefix="webdl-hls")
 
-    print "Downloading: %s" % filename
-
     try:
         best_stream_url = download_hls_get_stream(hack_url_func(m3u8_master_url))
-        ts_file = open(filename, "w")
+        ts_file = open(filename, "wb")
         download_hls_segments(ts_file, hack_url_func(best_stream_url))
     except KeyboardInterrupt:
         print "\nCancelled", m3u8_master_url