Repository: ytdl-org/youtube-dl

Issue:
best[height=] downloads Video-Only DASH when height is only available in DASH...
As requested, here is a verbose output of a youtube 'best[height=]' option that is downloading video-only DASH.  As stated before, I was able to get around it by doing '<=' instead of '='.   This seems to be a script-glitch from asking it to pull a resolution that is only available in DASH.

---

youtube-dl -v -f best[height=480] https://www.youtube.com/watch?v=_PNBF_LGrfM
[debug] System config: []
[debug] User config: []
[debug] Command-line args: [u'-v', u'-f', u'best[height=480]', u'https://www.youtube.com/watch?v=_PNBF_LGrfM']
[debug] Encodings: locale US-ASCII, fs utf-8, out US-ASCII, pref US-ASCII
[debug] youtube-dl version 2015.04.28
[debug] Python version 2.7.8 - Darwin-8.11.0-Power_Macintosh-powerpc-32bit
[debug] exe versions: none
[debug] Proxy map: {}
[youtube] _PNBF_LGrfM: Downloading webpage
[youtube] _PNBF_LGrfM: Extracting video information
[youtube] _PNBF_LGrfM: Downloading DASH manifest
[debug] Invoking downloader on 'https://r6---sn-qxo7sn7k.googlevideo.com/videoplayback?id=fcf34117f2c6adf3&itag=135&source=youtube&requiressl=yes&mm=31&ms=au&mv=m&pl=16&ratebypass=yes&mime=video/mp4&gir=yes&clen=27477814&lmt=1430256318389239&dur=339.714&mt=1430327681&key=dg_yt0&fexp=900720,907263,934954,938028,9406690,9407588,9407877,9408007,9408705,9408710,9408787,9409205,9409230,9412839,9413011,9413149,947233,948124,952612,952637,957201&sver=3&upn=tPFdYxEOYkM&signature=6F146691843968AD129BD05B140922A1ABD65D96.97DE5911E48E99520EE5208297519740AF696615&ip=97.115.165.13&ipbits=0&expire=1430349373&sparams=ip,ipbits,expire,id,itag,source,requiressl,mm,ms,mv,pl,ratebypass,mime,gir,clen,lmt,dur'
[download] Destination: Classic Game Room - QUICK STEP review for Atari 2600-_PNBF_LGrfM.mp4
[download] 100% of 26.20MiB in 00:34



Discussion:
There are no _exactly-480-height_ non-DASH videos by that link. What would you expect to happen in this case? Throwing an error that requested format is not available?

I know.  jaimeMF asked me to post this as a new issue.  They said that best[height] should not pass DASH and want to track it down.

Cross-referencing issue #5550

@jaimeMF what do you think should we do? Throwing format not available error may break use flow of audio only extractors like soundcloud. Implementing `best` similarly to [`worst`](https://github.com/rg3/youtube-dl/blob/master/youtube_dl/YoutubeDL.py#L920-L925) will still end up in fallback to audio/video-only formats when filter is not satisfied for audiovideo formats.

I thought that `best` would only select non-DASH formats, but it doesn't (and it have never worked like that). Maybe we should do like with `worst`, but without falling back to DASH formats. For audio only extractors it should work as now, I have to look if it can be easily implemented.

It's just my opinion, maybe you disagree.


Resolving patch:
diff --git a/youtube_dl/YoutubeDL.py b/youtube_dl/YoutubeDL.py
--- 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


Test patch:
diff --git a/test/test_YoutubeDL.py b/test/test_YoutubeDL.py
--- 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)