Make 'best' format only match non-DASH formats (closes #5554)
diff --git a/test/test_YoutubeDL.py b/test/test_YoutubeDL.py
index bb4a65ee182..82b827536d7 100644
--- a/test/test_YoutubeDL.py
+++ b/test/test_YoutubeDL.py
@@ -237,7 +237,7 @@ def test_youtube_format_selection(self):
             f2['url'] = 'url:' + f2id
 
             info_dict = _make_result([f1, f2], extractor='youtube')
-            ydl = YDL()
+            ydl = YDL({'format': 'best/bestvideo'})
             yie = YoutubeIE(ydl)
             yie._sort_formats(info_dict['formats'])
             ydl.process_ie_result(info_dict)
@@ -245,7 +245,7 @@ def test_youtube_format_selection(self):
             self.assertEqual(downloaded['format_id'], f1id)
 
             info_dict = _make_result([f2, f1], extractor='youtube')
-            ydl = YDL()
+            ydl = YDL({'format': 'best/bestvideo'})
             yie = YoutubeIE(ydl)
             yie._sort_formats(info_dict['formats'])
             ydl.process_ie_result(info_dict)
diff --git a/youtube_dl/YoutubeDL.py b/youtube_dl/YoutubeDL.py
index 827c88e0d9e..eee9c015435 100755
--- a/youtube_dl/YoutubeDL.py
+++ b/youtube_dl/YoutubeDL.py
@@ -915,7 +915,14 @@ def select_format(self, format_spec, available_formats):
             return None
 
         if format_spec == 'best' or format_spec is None:
-            return available_formats[-1]
+            audiovideo_formats = [
+                f for f in available_formats
+                if f.get('vcodec') != 'none' and f.get('acodec') != 'none']
+            if audiovideo_formats:
+                return audiovideo_formats[-1]
+            # for audio only urls, 'best' selects the best audio format
+            elif all(f.get('acodec') != 'none' for f in available_formats):
+                return available_formats[-1]
         elif format_spec == 'worst':
             audiovideo_formats = [
                 f for f in available_formats

Otherwise it's impossible to only download non-DASH formats, for example `best[height=?480]/best` would download a DASH video if it's the only one with height=480, instead for falling back to the second format specifier.
For audio only urls (soundcloud, bandcamp ...), the best audio will be downloaded as before.
