diff options
author | NeodarZ <neodarz@neodarz.net> | 2017-07-10 00:06:40 +0200 |
---|---|---|
committer | NeodarZ <neodarz@neodarz.net> | 2017-07-10 00:06:40 +0200 |
commit | e1cec6970766fa4f1b8f47fb886dcd640cf51d94 (patch) | |
tree | febcb0c738e8b5a1e1de6ad087d5e659fca8ce26 /convert.sh | |
download | video_save-e1cec6970766fa4f1b8f47fb886dcd640cf51d94.tar.xz video_save-e1cec6970766fa4f1b8f47fb886dcd640cf51d94.zip |
First commit
Diffstat (limited to '')
-rwxr-xr-x | convert.sh | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/convert.sh b/convert.sh new file mode 100755 index 0000000..ae8ea31 --- /dev/null +++ b/convert.sh @@ -0,0 +1,100 @@ +#!/usr/bin/env python3 + +import sys +import os +import subprocess +import re +#from subprocess import PIPE, CalledProcessError, check_call, Popen +from subprocess import check_output, STDOUT, CalledProcessError, Popen, PIPE +import pexpect +import time +import progressbar +import datetime + + +sourcedir = "/home/neodarz/video_save/ZXAAA Demo ZX Spectrum/Cracktro" +outdir = "/home/neodarz/video_save/ZXAAA Demo ZX Spectrum_mp3/Cracktro" + +def dirSize(path,ext1,ext2): + list_dir = [] + list_dir = os.listdir(path) + count = 0 + for file in list_dir: + if file.endswith(ext1): + count += 1 + if file.endswith(ext2): + count += 1 + return count + +total = dirSize(sourcedir, ".mp4", ".mkv") + +count = 0 +for file in sorted(os.listdir(sourcedir)): + try: + name = file[:file.rfind(".")] + if file.endswith(".mp4"): + print("Converting : "+name) + count += 1 + #cmd = ["ffmpeg", "-n", "-i", sourcedir+"/"+name+".mp4", "-c:a", "libmp3lame", outdir+"/"+name+".mp3"]² + cmd = "ffmpeg -y -i '"+sourcedir+"/"+name+".mp4' -c:a libmp3lame '"+outdir+"/"+name+".mp3'" + + thread = pexpect.spawn(cmd) + cpl = thread.compile_pattern_list([pexpect.EOF,"frame= *\d+",'(.+)']) + + while True: + i = thread.expect_list(cpl, timeout=None) + if i == 0: # EOF + break + elif i == 1: + time_number = thread.match.group(0) + print(time_number) + thread.close + elif i == 2: + unknown_line = thread.match.group(0) + if re.match(r'.*Duration.*', str(unknown_line)): + count = 0 + for element in unknown_line.split(): + count += 1 + if re.match(r"^b'Duration:'$", str(element)): + break + total = str(unknown_line.split()[count]).split("'")[1].split(',')[0] + totalRange = time.strptime(total.split('.')[0],'%H:%M:%S') + totalRangeS = datetime.timedelta(hours=totalRange.tm_hour,minutes=totalRange.tm_min,seconds=totalRange.tm_sec).total_seconds() + if re.match(r'.*time=.*', str(unknown_line)): + #print(str(unknown_line)+total) + #print(str(unknown_line).split("=")[2].split()[0]+"/"+total) + timeRange = time.strptime(str(unknown_line).split("=")[2].split()[0].split('.')[0],'%H:%M:%S') + timeRangeS = datetime.timedelta(hours=timeRange.tm_hour,minutes=timeRange.tm_min,seconds=timeRange.tm_sec).total_seconds() + print(str(round(100*timeRangeS/totalRangeS, 1))+"% / 100%", end='\r') + + #else if print(unknown_line) # for show other type of error + thread.close + #pass + + + + + + + + #p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE) + + #for line in p.stdout: + # print(str(line.rstrip())) + #p.stdout.flush() + + #try: + # print(str(count)+"/"+str(total)) + #output = check_output(["ffmpeg", "-n", "-i", sourcedir+"/"+name+".mp4", "-c:a", "libmp3lame", outdir+"/"+name+".mp3"], stderr=STDOUT) + #except CalledProcessError as e: + + #except CalledProcessError as e: + # print(e.message) + # if os.path.getsize(outdir+"/"+name+".mp3") < os.path.getsize(sourcedir+"/"+name+".mp4") and re.search(r'.*[already exists].*', str(e)): + # print("\n\nout: "+str(os.path.getsize(outdir+"/"+name+".mp3"))+" < source:"+str(os.path.getsize(sourcedir+"/"+name+".mp4"))) + # print("BAD") + #sys.exit(1) + except KeyboardInterrupt: + # Read the comment here => https://stackoverflow.com/a/1112357 ! + os.remove(outdir+"/"+name+".mp3") + sys.exit() |