Csound Csound-dev Csound-tekno Search About

[Csnd-dev] Mike's Youtube script for Linux

Date2016-12-21 18:58
FromDave Seidel
Subject[Csnd-dev] Mike's Youtube script for Linux
I took Mike's very useful Python script that creates MP4 videos with a static spectrogram image, ported it to Linux, and did some compulsive refactoring and generalization. I have some more work I want to do on it (e.g., I changed the spectrogram to remove the axes and legends, because I prefer it that way, but the user should have an option for this), and will eventually put it on GitHub, but here's what I have now. Note that the new script takes the name of a text file where all the metadata (and the sounds filename) is specified as simple key=value pairs like so:

[__track__]
label=hexany_catalog_part1
soundfile=hexany_catalog_part1_master.wav
album=Hexany Permutations
title=Part 1
track=1
year=2016
artist=Dave Seidel
composer=Dave Seidel
genre=Electroacoustic
publisher=Mysterybear Music, ASCAP
copyright=Copyright (c) 2016 by Dave Seidel, some rights reserved. Licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)
recorded=2016-12-18 15:10:58-05:00
encoded=2016-12-18 15:10:58-05:00
tagged=2016-12-18 15:10:58-05:00

where the "__track__" header is mandatory.

Comments and suggestions are welcome. Also, I think it would not be too hard to make this script work cross-platform, but I have only Linux here.

I am calling it "ytrender.py", but am open to a better name.

- Dave

import argparse
import datetime
import os
import os.path
import sys
import ConfigParser
#
# generate spectrogram image (png)
#
def gen_spectrogram(meta, raw_graph=False):
    sox_spectrogram_command = 'sox %s' % ' '.join((
        '-S "%s"' % meta["soundfile"],
        '-n spectrogram',
        '-r' if raw_graph else '',
        '-o "%s.png"' % meta["label"],
        '-t "%s"' % "wav",
        '-c "%s"' % "%s (%s)" % (meta["copyright"], meta["publisher"])
    ))
    return os.system(sox_spectrogram_command)
#
# generate video (mp4)
#
def gen_video(meta):
    cwd = os.getcwd()
    mp4_metadata = ' '.join((
        '-metadata title="%s: %s"'  % (meta["album"], meta["title"]),
        '-metadata album="%s"'      % meta["album"],
        '-metadata date="%s"'       % meta["year"],
        '-metadata track="%s"'      % meta["track"],
        '-metadata genre="%s"'      % meta["genre"],
        '-metadata publisher="%s"'  % meta["publisher"],
        '-metadata copyright="%s"'  % meta["copyright"],
        '-metadata composer="%s"'   % meta["composer"],
        '-metadata artist="%s"'     % meta["artist"]
    ))
    mp4_command = 'ffmpeg %s' % ' '.join((
        '-loglevel info',
        '-hide_banner',
        '-y',
        '-loop 1',
        '-framerate 2',
        '-i "%s"' % os.path.join(cwd, "%s.png" % meta["label"]),
        '-i "%s"' % os.path.join(cwd, meta["soundfile"]),
        '-c:v libx264',
        '-preset medium',
        '-tune stillimage',
        '-crf 18',
        '-codec:a aac',
        '-strict -2',
        '-b:a 384k',
        '-r:a 48000',
        '-shortest',
        '-pix_fmt yuv420p',
        '-vf "scale=trunc(iw/2)*2:trunc(ih/2)*2"',
        '%s' % mp4_metadata,
        '"%s"' % os.path.join(cwd, "%s.mp4" % meta["label"])
    ))
    return os.system(mp4_command)
    
    
def main(argv):
    metafile = argv[0]
    config = ConfigParser.RawConfigParser()
    config.read(metafile)
    # convert list of tuples to dict
    meta = dict(config.items("__track__"))
    
    print "[[[ Generating spectrogram... ]]]"
    gen_spectrogram(meta)
    print "[[[ Generating video... ]]]"
    gen_video(meta)
    
    print "[[[ Done! ]]]"
    return 0

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print "Usage: ytrender METADATA_FILE"
        sys.exit(1)
    
    sys.exit(main(sys.argv[1:]))


Date2016-12-21 23:46
FromKelly Hirai
SubjectRe: [Csnd-dev] Mike's Youtube script for Linux
oh man, i like this. its getting so hard to publish music without some 
visual component. can't wait to try it out.

k.

On 12/21/2016 01:58 PM, Dave Seidel wrote:
> I took Mike's very useful Python script that creates MP4 videos with a
> static spectrogram image, ported it to Linux, and did some compulsive
> refactoring and generalization. I have some more work I want to do on it
> (e.g., I changed the spectrogram to remove the axes and legends, because I
> prefer it that way, but the user should have an option for this), and will
> eventually put it on GitHub, but here's what I have now. Note that the new
> script takes the name of a text file where all the metadata (and the sounds
> filename) is specified as simple key=value pairs like so:
>
> [__track__]
>> label=hexany_catalog_part1
>> soundfile=hexany_catalog_part1_master.wav
>> album=Hexany Permutations
>> title=Part 1
>> track=1
>> year=2016
>> artist=Dave Seidel
>> composer=Dave Seidel
>> genre=Electroacoustic
>> publisher=Mysterybear Music, ASCAP
>> copyright=Copyright (c) 2016 by Dave Seidel, some rights reserved.
>> Licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0
>> Unported (CC BY-NC-SA 3.0)
>> recorded=2016-12-18 15:10:58-05:00
>> encoded=2016-12-18 15:10:58-05:00
>> tagged=2016-12-18 15:10:58-05:00
>
>
> where the "__track__" header is mandatory.
>
> Comments and suggestions are welcome. Also, I think it would not be too
> hard to make this script work cross-platform, but I have only Linux here.
>
> I am calling it "ytrender.py", but am open to a better name.
>
> - Dave
>
> import argparse
>> import datetime
>> import os
>> import os.path
>> import sys
>> import ConfigParser
>> #
>> # generate spectrogram image (png)
>> #
>> def gen_spectrogram(meta, raw_graph=False):
>>     sox_spectrogram_command = 'sox %s' % ' '.join((
>>         '-S "%s"' % meta["soundfile"],
>>         '-n spectrogram',
>>         '-r' if raw_graph else '',
>>         '-o "%s.png"' % meta["label"],
>>         '-t "%s"' % "wav",
>>         '-c "%s"' % "%s (%s)" % (meta["copyright"], meta["publisher"])
>>     ))
>>     return os.system(sox_spectrogram_command)
>> #
>> # generate video (mp4)
>> #
>> def gen_video(meta):
>>     cwd = os.getcwd()
>>     mp4_metadata = ' '.join((
>>         '-metadata title="%s: %s"'  % (meta["album"], meta["title"]),
>>         '-metadata album="%s"'      % meta["album"],
>>         '-metadata date="%s"'       % meta["year"],
>>         '-metadata track="%s"'      % meta["track"],
>>         '-metadata genre="%s"'      % meta["genre"],
>>         '-metadata publisher="%s"'  % meta["publisher"],
>>         '-metadata copyright="%s"'  % meta["copyright"],
>>         '-metadata composer="%s"'   % meta["composer"],
>>         '-metadata artist="%s"'     % meta["artist"]
>>     ))
>>     mp4_command = 'ffmpeg %s' % ' '.join((
>>         '-loglevel info',
>>         '-hide_banner',
>>         '-y',
>>         '-loop 1',
>>         '-framerate 2',
>>         '-i "%s"' % os.path.join(cwd, "%s.png" % meta["label"]),
>>         '-i "%s"' % os.path.join(cwd, meta["soundfile"]),
>>         '-c:v libx264',
>>         '-preset medium',
>>         '-tune stillimage',
>>         '-crf 18',
>>         '-codec:a aac',
>>         '-strict -2',
>>         '-b:a 384k',
>>         '-r:a 48000',
>>         '-shortest',
>>         '-pix_fmt yuv420p',
>>         '-vf "scale=trunc(iw/2)*2:trunc(ih/2)*2"',
>>         '%s' % mp4_metadata,
>>         '"%s"' % os.path.join(cwd, "%s.mp4" % meta["label"])
>>     ))
>>     return os.system(mp4_command)
>>
>>
>> def main(argv):
>>     metafile = argv[0]
>>     config = ConfigParser.RawConfigParser()
>>     config.read(metafile)
>>     # convert list of tuples to dict
>>     meta = dict(config.items("__track__"))
>>
>>     print "[[[ Generating spectrogram... ]]]"
>>     gen_spectrogram(meta)
>>     print "[[[ Generating video... ]]]"
>>     gen_video(meta)
>>
>>     print "[[[ Done! ]]]"
>>     return 0
>>
>> if __name__ == "__main__":
>>     if len(sys.argv) < 2:
>>         print "Usage: ytrender METADATA_FILE"
>>         sys.exit(1)
>>
>>     sys.exit(main(sys.argv[1:]))