Last active
December 20, 2015 12:29
-
-
Save UltraBob/6131132 to your computer and use it in GitHub Desktop.
a script to walk a directory looking for and unpacking compressed files, then find any movie files and pass them to a shell script that passes them through handbrake
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/python | |
import os | |
import subprocess | |
import sys | |
from pyunpack import Archive | |
""" TODO: | |
Make sure we don't delete files and folders that were added to the folder while we were processing, but were not processed yet themselves. | |
NOTE: if I decide to make the command line options more complex, look at the aregparse module | |
""" | |
if len(sys.argv) > 1: # if a path is provided as an argument, use the directory path as the starting point | |
if os.path.exists(sys.argv[1]): | |
if os.path.isfile(sys.argv[1]): | |
start_path = os.path.dirname(sys.argv[1]) | |
else: | |
start_path = sys.argv[1] | |
else: | |
start_path = os.getcwd() | |
else: | |
start_path = os.getcwd() | |
print "start path is " + start_path | |
archive_extensions = ['.cbr', '.bzip2','.7z','.ace','.alz','.a', '.arc', '.arj', '.bz2', '.cab', '.z', '.cpio', '.deb', '.dms', '.gz', '.lrz', '.lha', '.lzh', '.lz', '.lzma', '.lzo', '.rpm', '.rar', '.rz', '.tar', '.xz', '.zip', '.jar', '.zoo'] | |
extensions = ['.3gp', '.mp4', '.m4v', '.avi', '.wmv', '.asf', '.divx', '.mpeg', '.mpg', '.mov', '.dv'] | |
arch_files = [] | |
for root, dirs, files in os.walk(start_path): | |
for name in files: | |
for extension in archive_extensions: | |
if name.lower().endswith(extension): | |
arch_files.append(os.path.join(root, name)) | |
if len(arch_files) > 0: | |
print ("files to unzip: ", arch_files) | |
for file in arch_files: | |
Archive(file).extractall(os.path.dirname(file)) | |
vidwalk=[] | |
rmwalk=[] | |
for (root, dirs, files) in os.walk(start_path, topdown=False): # create a snapshot of currently folder and file situation to work with to avoid deleting late additions | |
rmwalk.append((root,dirs,files)) | |
for (root, dirs, files) in os.walk(start_path): # create a snapshot of currently folder and file situation to work with to avoid deleting late additions | |
vidwalk.append((root,dirs,files)) | |
vidfiles = [] | |
for root, dirs, files in vidwalk: | |
for name in files: | |
for extension in extensions: | |
if name.lower().endswith(extension): | |
vidfiles.append(os.path.join(root, name)) | |
vidfiles.insert(0,'/Users/moko/bin/converttotvcast.sh') | |
print("vidfiles: ",vidfiles) | |
subprocess.check_call(vidfiles) | |
for root, dirs, files in rmwalk: | |
for name in files: | |
os.remove(os.path.join(root, name)) | |
for name in dirs: | |
os.rmdir(os.path.join(root, name)) | |
print "done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment