Skip to content

Instantly share code, notes, and snippets.

@BlogBlocks
Last active December 19, 2017 00:25
Show Gist options
  • Save BlogBlocks/9468f303803c522da7d7cb25a8a598fa to your computer and use it in GitHub Desktop.
Save BlogBlocks/9468f303803c522da7d7cb25a8a598fa to your computer and use it in GitHub Desktop.
"""
Randomly choose an mp3 from a directory, randomly crop off some of the beginning: start = randint(20,70)
This prevents the long silent beginning some mp3s have. Also breaks the exact pattern in case of a
duplicate random choise. I use this generated file in another script so I save it with the same name:
music/use.mp3
That way I can use the same mp3 name in the app using TwitterPrep.py to generate a new one.
"""
import subprocess
from random import randint
import os
os.remove("music/use.mp3")
print("music/use.mp3 Removed!")
path = r"music/"
base_image = random.choice([
x for x in os.listdir(path)
if os.path.isfile(os.path.join(path, x))
])
filename0=(path+base_image)
print filename0
start = randint(20,70)
start = str(start)
# This fades the music in 2 second delay, fade for 7 seconds
out = "ffmpeg -ss "+start+" -i "+filename0+" -af afade=t=in:ss=2:d=7 -t 29 music/use.mp3"
subprocess.call(out, shell=True)
Audio("music/use.mp3", autoplay=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment