Merge pull request #8766 from yan12125/dash-detect-ext
[youtube-dl] / youtube_dl / utils.py
index 31d60f3233bb3ea95775f390d4f4afc0681b52d3..0e04e91a4202397b4a3a1c768f49827ac3a1a542 100644 (file)
@@ -465,6 +465,10 @@ def encodeFilename(s, for_subprocess=False):
     if not for_subprocess and sys.platform == 'win32' and sys.getwindowsversion()[0] >= 5:
         return s
 
+    # Jython assumes filenames are Unicode strings though reported as Python 2.x compatible
+    if sys.platform.startswith('java'):
+        return s
+
     return s.encode(get_subprocess_encoding(), 'ignore')
 
 
@@ -1215,13 +1219,23 @@ if sys.platform == 'win32':
             raise OSError('Unlocking file failed: %r' % ctypes.FormatError())
 
 else:
-    import fcntl
+    # Some platforms, such as Jython, is missing fcntl
+    try:
+        import fcntl
 
-    def _lock_file(f, exclusive):
-        fcntl.flock(f, fcntl.LOCK_EX if exclusive else fcntl.LOCK_SH)
+        def _lock_file(f, exclusive):
+            fcntl.flock(f, fcntl.LOCK_EX if exclusive else fcntl.LOCK_SH)
 
-    def _unlock_file(f):
-        fcntl.flock(f, fcntl.LOCK_UN)
+        def _unlock_file(f):
+            fcntl.flock(f, fcntl.LOCK_UN)
+    except ImportError:
+        UNSUPPORTED_MSG = 'file locking is not supported on this platform'
+
+        def _lock_file(f, exclusive):
+            raise IOError(UNSUPPORTED_MSG)
+
+        def _unlock_file(f):
+            raise IOError(UNSUPPORTED_MSG)
 
 
 class locked_file(object):
@@ -1385,6 +1399,12 @@ def fix_xml_ampersands(xml_str):
 
 def setproctitle(title):
     assert isinstance(title, compat_str)
+
+    # ctypes in Jython is not complete
+    # http://bugs.jython.org/issue2148
+    if sys.platform.startswith('java'):
+        return
+
     try:
         libc = ctypes.cdll.LoadLibrary('libc.so.6')
     except OSError:
@@ -1723,6 +1743,7 @@ def update_url_query(url, query):
     parsed_url = compat_urlparse.urlparse(url)
     qs = compat_parse_qs(parsed_url.query)
     qs.update(query)
+    qs = encode_dict(qs)
     return compat_urlparse.urlunparse(parsed_url._replace(
         query=compat_urllib_parse.urlencode(qs, True)))
 
@@ -1872,6 +1893,22 @@ def mimetype2ext(mt):
     }.get(res, res)
 
 
+def codec2ext(codec):
+    codec_type = codec.split('.')[0]
+
+    # Leave the return value None for unknown values as codec_type
+    # is not a good fallback for file extensions
+    return {
+        'avc1': 'mp4',
+        'avc2': 'mp4',
+        'avc3': 'mp4',
+        'avc4': 'mp4',
+        'mp4a': 'm4a',
+        'vorbis': 'webm',
+        'vp9': 'webm',
+    }.get(codec_type)
+
+
 def urlhandle_detect_ext(url_handle):
     try:
         url_handle.headers