• etuomaala@sopuli.xyz
    link
    fedilink
    arrow-up
    1
    ·
    1 year ago
    I download opus .webm s,
        then use ffmpeg to convert to .opus,
        which I'm pretty sure is just ogg.
    IDK, works great in quodlibet.
    .webm can't be tagged, though.
    Tagging is critical, especially replay_gain tags.
    
    Here is my script:
    #!/usr/bin/env python
    
    # Youtube actually hosts audio-only opus tracks, but you can only get them
    # in the webm container, which many music players, including quodlibet, don't
    # know what to do with. This script downloads the track, then converts it with
    # zero loss to the opus container using ffmpeg's `-acodec copy` feature.
    
    import sys
    from subprocess import call
    from os.path import splitext
    from os import remove, walk, listdir
    from tempfile import TemporaryDirectory
    from shutil import move
    
    urls = sys.argv[1:]
    
    with TemporaryDirectory(prefix='yta-') as tempdir:
        # Do not raise exceptions, because yt-dlp counts failure of a single
        # download in the list as an overall failure, exit code wise.
        call(['env', '-C', tempdir, 'yt-dlp', '-if', 'bestaudio',
            '-o', '%(artist)s - %(title)s - %(id)s.%(ext)s', '--'] + urls)
    
        for tempdir, dirs, files in walk(tempdir):
            for fn in files:
                path = tempdir+'/'+fn
                name, ext = splitext(path)
                if ext == '.webm':
                    if call([
                        'ffmpeg', '-hide_banner',
                        '-i', path,
                        '-acodec', 'copy',
                        name+'.opus'
                    ]) == 0:
                        remove(path)
    
        for node in listdir(tempdir):
            move(tempdir+'/'+node, '.')