SHOW:
|
|
- or go back to the newest paste.
| 1 | #!/usr/bin/python | |
| 2 | #/home/clockworkpc/bin/tubemixingsave.py | |
| 3 | ||
| 4 | # Released under a GPLv3 Licence by Clockwork PC 2013 | |
| 5 | # www.clockworkpc.com.au | |
| 6 | ||
| 7 | # You are entitled to the following four freedoms: | |
| 8 | # Freedom 0: To run this program for any purpose | |
| 9 | # Freedom 1: To study how this program works and change it to make it do what you wish | |
| 10 | # Freedom 2: To redistribute copies so you can help your neighbour | |
| 11 | # Freedom 3: To distribute copies of your modified version to others | |
| 12 | ||
| 13 | import os, datetime, re, shutil | |
| 14 | ||
| 15 | # Create time-stamp string for folder and file names | |
| 16 | now = datetime.datetime.now() | |
| 17 | nowstring = str(now.strftime("%Y-%m-%d_%A_%H%M%S"))
| |
| 18 | - | #print nowstring |
| 18 | + | |
| 19 | # Define the folders and file names to be used | |
| 20 | tubemixingFolder = "/tmp/tubemixing/" | |
| 21 | videoFolder = os.getenv("HOME") + "/Videos/"
| |
| 22 | saveFolder = videoFolder + "tubemixing/" | |
| 23 | originalVideosFolder = saveFolder + "OriginalVideos/" | |
| 24 | namedVideosFolder = saveFolder + "NamedVideos/" | |
| 25 | namedVideosFolderDateStamped = namedVideosFolder + nowstring + "/" | |
| 26 | videoListsFolder = saveFolder + "VideoLists/" | |
| 27 | videoListsFile = videoListsFolder + nowstring | |
| 28 | videoDataFolder = saveFolder + "gdata_text_files/" | |
| 29 | videoDataFolderDatestamped = saveFolder + "gdata_text_files/" + nowstring + "/" | |
| 30 | mp3Folder = os.getenv("HOME") + "/Music/tubemixing/" + nowstring + "/"
| |
| 31 | gdataFeed = "http://gdata.youtube.com/feeds/api/videos/" | |
| 32 | originalVideosList = originalVideosFolder + "original_videos_list" | |
| 33 | originalVideosListBackup = originalVideosFolder + "original_videos_list_" + nowstring + ".backup" | |
| 34 | ||
| 35 | - | #Check for new videos in Tubemixing /tmp folder |
| 35 | + | |
| 36 | - | def check_for_video_list(): |
| 36 | + | |
| 37 | - | running = True |
| 37 | + | |
| 38 | - | for names in os.listdir(originalVideosFolder): |
| 38 | + | |
| 39 | - | if names.startswith("original_videos_list"):
|
| 39 | + | |
| 40 | - | if not names.endswith(".backup"):
|
| 40 | + | |
| 41 | - | print "found original" |
| 41 | + | |
| 42 | - | os.system("cp -v " + originalVideosList + " " + originalVideosListBackup)
|
| 42 | + | |
| 43 | - | running = False |
| 43 | + | |
| 44 | - | check_for_video_list() |
| 44 | + | |
| 45 | e = [x for x in a if x not in d] | |
| 46 | print e | |
| 47 | ||
| 48 | #Replenish list | |
| 49 | #f = open(originalVideosList, "a") | |
| 50 | #f.write(str(os.listdir(originalVideosFolder))) | |
| 51 | #f.close() | |
| 52 | ||
| 53 | # Match only videos that haven't already been saved (replaced) | |
| 54 | #a = os.listdir(tubemixingFolder) | |
| 55 | #b = os.listdir(originalVideosFolder) | |
| 56 | #c = [x for x in a if x not in b] | |
| 57 | ||
| 58 | # File-only os.listdir | |
| 59 | def saveFolder_files_list(): | |
| 60 | for file in os.listdir(saveFolder): | |
| 61 | if os.path.isfile(os.path.join(saveFolder,file)): | |
| 62 | return file | |
| 63 | ||
| 64 | def files_only(saveFolder): | |
| 65 | return [filename for filename in os.listdir(saveFolder) if os.path.isfile(os.path.join(saveFolder, filename))] | |
| 66 | ||
| 67 | def files_only(videoDataFolderDatestamped): | |
| 68 | return [filename for filename in os.listdir(videoDataFolderDatestamped) if os.path.isfile(os.path.join(videoDataFolderDatestamped, filename))] | |
| 69 | ||
| 70 | # Copy NEW videos from Tubemixing temp folder to working folder | |
| 71 | #for file in c: | |
| 72 | for file in e: | |
| 73 | shutil.copyfile(tubemixingFolder+file,saveFolder+file) | |
| 74 | # Update list of saved videos | |
| 75 | g = open(originalVideosList, "a") | |
| 76 | g.write(str(e)) | |
| 77 | g.close() | |
| 78 | ||
| 79 | # Copy NEW videos to archive folder for future reference (replaced) | |
| 80 | # shutil.copyfile(tubemixingFolder+file,originalVideosFolder+file) | |
| 81 | ||
| 82 | # Remove prefix | |
| 83 | # NOTE: I set Tubemixing to save by default to 360p. | |
| 84 | # You'll have to tweak it if you save higher resolution by default. | |
| 85 | preFix1 = "tubemixing-360p-" | |
| 86 | preFix2 = "tubemixing--" | |
| 87 | ||
| 88 | #os.listdir(saveFolder) | |
| 89 | for filename in files_only(saveFolder): | |
| 90 | if filename.startswith(preFix1): | |
| 91 | os.rename(saveFolder+filename,saveFolder+filename[16:]) | |
| 92 | ||
| 93 | # Second pass (some files are saved with preFix2) | |
| 94 | os.listdir(saveFolder) | |
| 95 | for filename in files_only(saveFolder): | |
| 96 | if filename.startswith(preFix2): | |
| 97 | os.rename(saveFolder+filename,saveFolder+filename[12:]) | |
| 98 | ||
| 99 | # Use gdata to fetch video title from YouTube | |
| 100 | for filename in files_only(saveFolder): | |
| 101 | if len(filename) == 11: | |
| 102 | os.system("cd " + videoDataFolderDatestamped + " && wget " + gdataFeed + filename + " " + "-O" + " " + filename)
| |
| 103 | ||
| 104 | # Parse video title from data fetched from YouTube | |
| 105 | print files_only(videoDataFolderDatestamped) | |
| 106 | for filename in files_only(videoDataFolderDatestamped): | |
| 107 | f = open(videoDataFolderDatestamped+filename) | |
| 108 | for line in f: | |
| 109 | if "<title type='text'>" in line: | |
| 110 | x = line | |
| 111 | r = re.compile("<title type='text'>(.*?)</title><content")
| |
| 112 | m = r.search(x) | |
| 113 | if m: | |
| 114 | titleInfo = m.group(1) | |
| 115 | print titleInfo | |
| 116 | # Using video title, save properly named and suffixed files in a new date-stamped folder | |
| 117 | # Using os.system instead of shutil because shutil is a FUCKING SLOW MEMORY HOG | |
| 118 | os.system("cp -v " + saveFolder+filename + " " + '"' + namedVideosFolderDateStamped+titleInfo+".webm" + '"')
| |
| 119 | # shutil.copyfile(saveFolder+filename,namedVideosFolderDateStamped+titleInfo+".webm") | |
| 120 | ||
| 121 | # Remove code-named video files, because originals have already been saved for reference | |
| 122 | # Unfortunately, the best I can do is use the clumsy rm command, which tries, but fails, to delete folders. This is not the way to go, but I can't find a more elegant way of clearing the working folder. | |
| 123 | ||
| 124 | for file in os.listdir(saveFolder): | |
| 125 | os.system("rm -v " + saveFolder + file)
| |
| 126 | ||
| 127 | # Use AVconv/FFmpeg to extract MP3 audio into the Music folder | |
| 128 | for filename in os.listdir(namedVideosFolderDateStamped): | |
| 129 | (str, filename) | |
| 130 | namedVideoFile = namedVideosFolderDateStamped + filename | |
| 131 | print namedVideoFile | |
| 132 | ||
| 133 | # os.system("avconv -i " + '"' + namedVideoFile + '"' + " " + '"' + mp3Folder + filename + '.mp3"')
| |
| 134 | os.system("ffmpeg -i " + '"' + namedVideoFile + '"' + " " + '"' + mp3Folder + filename + '.mp3"')
| |
| 135 | ||
| 136 | # Remove the ".webm" that carries over to the MP3 file name during the AVconv conversion | |
| 137 | ||
| 138 | - | # Use AVconv (FFmpeg) to extract MP3 audio into the Music folder |
| 138 | + | |
| 139 | for root, _, filenames in os.walk(mp3Folder) | |
| 140 | for filename in filenames | |
| 141 | ) | |
| 142 | for path in pathiter: | |
| 143 | newname = path.replace('.webm', '')
| |
| 144 | - | os.system("avconv -i " + '"' + namedVideoFile + '"' + " " + '"' + mp3Folder + filename + '.mp3"')
|
| 144 | + | |
| 145 | os.rename(path,newname) | |
| 146 | ||
| 147 | # Clear empty folders from directories that tend to get cluttered | |
| 148 | work_path1 = os.getenv("HOME") + "/Videos/tubemixing/NamedVideos/"
| |
| 149 | work_path2 = os.getenv("HOME") + "/Music/tubemixing/"
| |
| 150 | ||
| 151 | def clear_named_videos_folders(): | |
| 152 | for file in os.listdir(work_path1): | |
| 153 | if not os.listdir(work_path1+file): | |
| 154 | print "empty" | |
| 155 | os.rmdir(work_path1+file) | |
| 156 | else: | |
| 157 | print "not empty" | |
| 158 | ||
| 159 | def clear_avconv_mp3_folders(): | |
| 160 | for file in os.listdir(work_path2): | |
| 161 | if not os.listdir(work_path2+file): | |
| 162 | print "empty" | |
| 163 | os.rmdir(work_path2+file) | |
| 164 | else: | |
| 165 | print "not empty" | |
| 166 | ||
| 167 | clear_named_videos_folders() | |
| 168 | clear_avconv_mp3_folders() |