[PYTHON] convert youtube playlist to local m3u format file for smplayer (by toy tool)

Before accepting

-mplayer (smplayer) is from YouTube It understands the page URL and plays the video, but you need to create a local playlist file in advance to play multiple videos continuously. --I'm not happy to play Youtube playlists in your browser. -youtube-dl can download all Youtube playlists locally, but there is a way to create a list without downloading- Only the s option can be found, which lists the temporary URLs of the video itself. It seems unlikely that you can list permanent URLs from the command line unless they are deleted. --Youtube-dl The command is actually a python script, and the command calls the python library.

Now let's call the youtube-dl library from command line python and create a local playlist file that can be used for a while.

environment

This time, I checked the operation while making it on Windows 10, using gitbash as the shell and Conda Python installed with /), youtube-dl installed from that pip.

Body

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import codecs
import sys
import youtube_dl

class yt_Listup:
    """ Wrapper for youtube-dl library
      * read youtube.com playlist
      * output by m3u format 
    """

    def __init__(self, url):
        self.url_list = []
        ydl = youtube_dl.YoutubeDL({'outtmpl': u'%(id)s%(ext)s', \
                'logtostderr': True})
        with ydl:
            result = ydl.extract_info(
                url ,
                download=False, # We just want to extract the info
            )

        if 'entries' in result:
            self.url_list.extend(result['entries'])
        else:
            self.url_list.append(result)

    def m3u(self, fileHandle = codecs.getwriter('utf_8')(sys.stdout)):
        for item in self.url_list:
            fileHandle.write( "# " + item['title'] + "\n" + \
                    item['webpage_url'] + "\n" )
        return


if __name__ == "__main__" :
    l = yt_Listup(sys.argv[1]).m3u()

For the time being, set the output destination to stdout and save it by redirect. …… I feel that people who need a proviso do not read code fragments, so it's a shame.

It should be noted that

To be scolded by people who are serious about python, it has not been formatted to comply with PEP8. It doesn't include test code, which is likely to be scolded by people who are serious about development. If you don't want to extend this feature, it's simpler and more correct to assign with = without using append () or extend (). My preference is to add a Usage () function for command line use, rename this code and eventually put it in the library path. Aside from the playback screen, I didn't like the URL being displayed as it was on smplayer's playlist screen, and I made some modifications from the code given here. The more multifunctional it is, the longer it becomes.

On top of that, I couldn't find the "likely for everyone" feature of saving youtube playlists to local files. The main part is the easiest to understand, and I thought it would be easier to read if only this code was published because it was close to the glue of the site called Qiita.

Thanks

Recommended Posts

convert youtube playlist to local m3u format file for smplayer (by toy tool)
Convert matplotlib graphs to emf file format
Convert binary packages for windows to wheel format
Convert Qiita articles to Jekyll post format for backup
How to convert Json file to CSV format or EXCEL format
Convert Pascal VOC format xml file to COCO format json file
[Caffe] Convert mean file from binary proto format to npy format
Convert Excel file to text in Python for diff purposes