Last active
March 8, 2019 17:36
-
-
Save marios8543/4b1d6d93e2bf675929b741c28e8f073b to your computer and use it in GitHub Desktop.
A simple program to convert to WebM and create videos from audio tracks. I made to use this to post on /wsg/. Requires AppJar and Mutagen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3.7 | |
from appJar import gui | |
from subprocess import run,PIPE | |
from os.path import join | |
from os import remove | |
from mutagen import File,flac | |
app = gui(title="Lazy WebM") | |
res = {"1920x1080":"1920:1080","1280x720":"1280:720","720x480":"720:480","640x480":"640:480","640x360":"640:360","480x360":"480:360"} | |
def run_ffmpeg(com): | |
ret = run(com,shell=True,text=True,stderr=PIPE) | |
if ret.returncode==0: | |
app.infoBox("Done", "Completed successfully", parent=None) | |
else: | |
app.errorBox("Something went wrong ({})".format(ret.returncode),ret.stderr) | |
def convertWebm(): | |
input_file = app.getEntry("if") | |
output_file = join("/".join(input_file.split("/")[:-1]),"{}.webm".format(app.getEntry("output_name") if app.getEntry("output_name") else input_file.split(".")[-2])) | |
codec = "libvpx-vp9" if app.getRadioButton("codec")=="VP9" else "libvpx" | |
acodec = "-acodec {}".format(app.getRadioButton("acodec").lower()) if app.getRadioButton("acodec") else "" | |
resolution = "-vf scale={}".format(res[app.getOptionBox("Resolution: ")]) if app.getOptionBox("Resolution: ") in res else "" | |
lossless = "-lossless 1" if app.getCheckBox(" Lossless") and codec=="libvpx-vp9" else "" | |
start_time = "-ss {}".format(app.getEntry("start_point")) if app.getEntry("start_point") else "" | |
finish_time = "-t {}".format(app.getEntry("finish_point")) if app.getEntry("finish_point") else "" | |
fps = "-r {}".format(int(app.getEntry("frame_rate"))) if app.getEntry("frame_rate") else "" | |
bitrate = "-b:v {}M".format(app.getEntry("bit_rate")) if app.getEntry("bit_rate") else "" | |
quality = app.getOptionBox("Deadline / Quality: ").lower() | |
print(input_file,output_file) | |
return run_ffmpeg("ffmpeg -y -i '{input_file}' {fps} {bit_rate} {start_time} {finish_time} -c:v {codec} {acodec} {lossless} -deadline {quality} {resolution} '{output_file}'".format(**{'input_file':input_file,'codec':codec,'quality':quality,'resolution':resolution,'output_file':output_file,'lossless':lossless,'start_time':start_time,'finish_time':finish_time,'fps':fps,'bit_rate':bitrate,'acodec':acodec})) | |
def createGroove(): | |
input_path = app.getEntry("groove_audio") | |
if not app.getEntry("groove_image") or app.getEntry("groove_image")=="": | |
input_file = File(input_path) | |
ext = input_path.split(".")[-1] | |
if ext=='mp3': | |
img_ext = 'jpg' | |
artwork = input_file.tags['APIC:'].data if 'APIC:' in input_file.tags and len(input_file.tags['APIC:'].data)>0 else None | |
elif ext=='flac': | |
pics = flac.FLAC(input_path).pictures | |
if len(pics)>0: | |
artwork = pics[0].data | |
img_ext = pics[0].mime.split("/")[-1] | |
else: | |
artwork = None | |
else: | |
artwork = None | |
if not artwork: | |
return app.errorBox("No artwork", "No picture file was specified and the song doesn't have a valid album art", parent=None) | |
with open("artwork.{}".format(img_ext),"wb") as img: | |
img.write(artwork) | |
img.flush() | |
img.close() | |
artwork = 'artwork.{}'.format(img_ext) | |
else: | |
artwork = app.getEntry("groove_image") | |
output = join("/".join(input_path.split("/")[:-1]),"{}.webm".format(app.getEntry("groove_output") if app.getEntry("groove_output") else input_path.split(".")[-2])) | |
print(output) | |
run_ffmpeg("ffmpeg -loop 1 -i '{image_file}' -i '{audio_file}' -r 1 -vf scale=256x256 -c:a libvorbis -c:v libvpx -b:v 0.1 -shortest -strict -2 '{out_name}'".format(**{'image_file':artwork,'audio_file':input_path,'out_name':output})) | |
if artwork=='artwork.jpg': | |
remove('artwork.jpg') | |
app.startTabbedFrame("main") | |
app.startTab("Convert to WebM") | |
app.addLabel("iflabel","Input file: ",0,0) | |
app.addFileEntry("if",row=0,column=1) | |
app.addLabel("codeclable","Video codec: ",1,0) | |
app.addRadioButton("codec","VP8",1,1) | |
app.addRadioButton("codec","VP9",1,2) | |
app.addLabel("codegfdclable","Audio codec: ",2,0) | |
app.addRadioButton("acodec","Libvorbis",2,1) | |
app.addRadioButton("acodec","Opus",2,2) | |
app.addRadioButton("acodec","MP3",2,3) | |
app.addLabelOptionBox("Resolution: ",["Same as original"]+list(res.keys()),3,1) | |
app.addLabel("fpslabel","Frame rate:",4,0) | |
app.addNumericEntry("frame_rate",4,1) | |
app.addLabel("bratelabel","Target bit rate: ",5,0) | |
app.addNumericEntry("bit_rate",5,1) | |
app.addLabel("startlable","Start: ",6,0) | |
app.addEntry("start_point",6,1,) | |
app.addLabel("endlable","End: ",6,2) | |
app.addEntry("finish_point",6,3,) | |
app.addCheckBox(" Lossless",7,1) | |
app.addLabelOptionBox("Deadline / Quality: ",["Good","Best","Realtime"],8,1) | |
app.addLabel("outlabel1","Output name: ",9,0) | |
app.addEntry("output_name",9,1) | |
app.addButton("Convert!", convertWebm,10,1) | |
app.stopTab() | |
app.startTab("Create groove") | |
app.addLabel("iflabel2","Audio file: ",0,0) | |
app.addFileEntry("groove_audio",row=0,column=1) | |
app.addLabel("iflabel3","Picture file (defaults to album art): ",1,0) | |
app.addFileEntry("groove_image",row=1,column=1) | |
app.addLabel("outlabel2","Output name: ",2,0) | |
app.addEntry("groove_output",2,1) | |
app.addButton("Create Groove!", createGroove,3,0) | |
app.stopTab() | |
app.stopTabbedFrame() | |
app.go() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment