def makeList(videos, name, verbose): printInfo1("\nCreating %s.%s..." % (name, listSuffix)) if continueWithProcess(name, listSuffix, True, False, "Will redownload\n", "Keeping old file. No download\n", verbose): listFile = codecs.open("%s.%s" % (name, listSuffix), "w", 'utf-8') for video in videos: if verbose: printInfo1("\nURL: %s" % video['url']) printInfo1("Season: %s" % video['season']) printInfo1("Episode title: %s" % video['episodeTitle']) listFile.write("%s\n" % video['url']) listFile.write("%s.%s.%s\n\n" % (name, video['season'], video['episodeTitle'])) listFile.close()
def convertVideo(videoInFile, convertTo, reEncode, verbose): keepOld = False reDownload = False fileAccepted = False fileName, fileExtension = os.path.splitext(videoInFile) fileExtension = fileExtension.lstrip(".") if verbose: printInfo1("File name: %s" % fileName) printInfo1("File extension: %s" % fileExtension) for extension in videoExtensions: if extension == fileExtension.lower(): fileAccepted = True if not fileAccepted: onError(15, "%s is probably not a video file" % videoInFile) if fileExtension.lower() == convertTo: printWarning("Same out format chosen as existing\nWill not convert") else: if verbose: printInfo2("Converting %s to %s format" % (videoInFile, convertTo)) printInfo2("Renaming %s to %s.bak" % (videoInFile, videoInFile)) os.rename(videoInFile, "%s.bak" % videoInFile) if fileExtension.lower() == "flv" and convertTo == "mp4": ffmpeg = getffmpegPath(verbose) if reEncode: if verbose: printInfo2("Reencoding video...") cmd = ("%s" " -i %s" " -qscale 0 -ar 22050 -vcodec %s" " '%s.%s'" % (ffmpeg, "%s.bak" % videoInFile, videoCodec, fileName, convertTo) ) else: cmd = ("%s" " -i %s -vcodec copy -acodec copy" " '%s.%s'" % (ffmpeg, "%s.bak" % videoInFile, fileName, convertTo) ) while True: printInfo1("Will convert") if continueWithProcess(fileName, fileExtension, keepOld, reDownload, "Will re convert\n", "Keeping old file\nNo converting\n", verbose): process = runProcess(cmd, verbose)
def finish(downloads, keepOld, reDownload, checkDuration, listOnly, convertTo, bashOutFile, verbose): shouldBeDeleted = [] if not listOnly: if downloads: infoDownloaded = getVideos(downloads, keepOld, reDownload, checkDuration, verbose) if convertTo: if verbose: printInfo2("Converting downloads...") convertDownloads(downloads, convertTo, verbose) else: infoDownloaded = "" onError(17, "Could not find any streams to download") else: infoDownloaded = "" printInfo1("\nListing only") printScores() if bashOutFile: if continueWithProcess(bashOutFile, bashSuffix, True, False, "Will redownload\n", "Keeping old file. No download\n", verbose): bashFile = open("%s.%s" % (bashOutFile, bashSuffix), "w") bashFile.write("#!/bin/bash\n\n") if downloads: printInfo1("These files would have been downloaded:") for line in downloads: # print line printInfo1("\nVideo name: %s.%s" % (line['name'].rstrip(), line['suffix'])) printInfo1("Video quality: %s" % line['quality']) printInfo1("Video address: %s" % line['address']) if line['subs']: printInfo1("Subtitles name: %s.srt" % line['name'].rstrip()) printInfo1("Subtitles address: %s" % line['subs']) else: printInfo1("No subtitles found") print "Duration: %s s" % line['duration'] if bashOutFile: if line['address'].startswith("http"): cmd = ffmpegDownloadCommand(line, verbose) bashFile.write("%s\n\n" % cmd) elif line['address'].startswith("rtmp"): cmd = rtmpdumpDownloadCommand(line, verbose) bashFile.write("%s\n\n" % cmd) if line['subs']: cmd = wgetDownloadCommand(line, verbose) bashFile.write("%s\n\n" % cmd) if bashOutFile: bashFile.close() st = os.stat("%s.sh" % bashOutFile) os.chmod("%s.sh" % bashOutFile, st.st_mode | stat.S_IEXEC) else: printWarning("Could not find anything that would have been downloaded") for line in infoDownloaded: printInfo1("\nVideo: %s" % line['videoName']) printScores() if line['expectedDuration'] != "0.000": printInfo1("Expected duration: %s" % (str(datetime.timedelta(seconds=round(float(line['expectedDuration']), 2))))) if verbose: printInfo1("Duration: %s ms" % line['duration']) printInfo1("Duration: %s" % line['durationFormatted']) if line['videoDlComment'] == dlCommentError: printError(line['videoDlComment']) shouldBeDeleted.append(line['videoName']) else: printInfo2(line['videoDlComment']) if verbose: printInfo1("Expected duration: %s s" % line['expectedDuration']) printInfo1("Actual duration: %s s" % line['duration']) if not compareDurations(float(line['expectedDuration']), float(line['duration']) / 1000, verbose): printError("Durations does not match") shouldBeDeleted.append(line['videoName']) if verbose: printInfo1("File size: %s b" % line['fileSize']) printInfo1("File size: %s" % line['fileSizeMeasure']) if verbose: printInfo1("Overall bit rate: %s bps" % line['overallBitRate']) printInfo1("Overall bit rate: %s" % line['overallBitRateMeasure']) print if verbose: printInfo1("Video format: %s" % line['videoFormat']) printInfo1("Video codec ID: %s" % line['videoCodecId']) printInfo1("Video bit rate: %s bps" % line['videoBitRate']) printInfo1("Video bit rate: %s" % line['videoBitRateMeasure']) printInfo1("Width: %s px" % line['width']) printInfo1("Height: %s px" % line['height']) printInfo1("Frame rate: %s fps" % line['frameRate']) if verbose: printInfo1("Frame count: %s" % line['frameCount']) print printInfo1("Audio format: %s" % line['audioFormat']) printInfo1("Audio codec ID: %s" % line['audioCodecId']) printInfo1("Audio bit rate: %s bps" % line['audioBitRate']) printInfo1("Audio bit rate: %s" % line['audioBitRateMeasure']) if line['subLines'] != 'na': printInfo1("\nSubtitles: %s" % line['subName']) printScores() if line['subDlComment'] == dlCommentError: printError(line['subDlComment']) shouldBeDeleted.append(line['subName']) else: printInfo2(line['subDlComment']) if line['expectedSubSize'] != "0": printInfo1("Expected file size: %s B" % line['expectedSubSize']) printInfo1("File size: %s B" % line['subSize']) printInfo1("Number of lines: %s" % line['subLines']) else: printWarning("\nNo subtitles downloaded") if shouldBeDeleted: print printError("\nThese files should be deleted and re downloaded") printScores() for line in shouldBeDeleted: printWarning(line) print
def getVideos(downloads, keepOld, reDownload, checkDuration, verbose): oldReDownload = reDownload printInfo2("\nStarting downloads") for line in downloads: trys = 0 videoCmd, subCmd = getDownloadCommands(line, verbose) while True: trys += 1 if trys > maxTrys: onError(29, "Tried to download video %s times\nSkipping..." % (trys - 1)) videoComment = dlCommentError break print printInfo2("Downloading stream to %s.%s ..." % (line['name'].rstrip(), line['suffix'])) expectedDuration = round(float(line['duration']), 2) printInfo1("Expected duration: %s" % str(datetime.timedelta(seconds=expectedDuration))) printInfo1("%s%s try" % (trys, numbering(trys, verbose))) printScores() if continueWithProcess(line['name'].rstrip(), line['suffix'], keepOld, reDownload, "Will redownload\n", "Keeping old file. No download\n", verbose): if verbose: printInfo2("Executing download command...") print videoCmd exitCode = runProcess(videoCmd, verbose) if exitCode != 0: printScores() onError(30, "Failed. Process exited on %s" % exitCode) printInfo2("Trying again...") reDownload = True else: if checkDuration and float(line['duration']) > 0: durationOK = checkDurations(line, verbose) else: if verbose: printWarning("Not checking duration") durationOK = True if os.path.isfile("%s.%s" % (line['name'].rstrip(), line['suffix'])) and durationOK: printScores() printInfo1("Finished downloading video") setPerms("%s.%s" % (line['name'].rstrip(), line['suffix']), verbose) reDownload = oldReDownload videoComment = dlCommentSuccess break else: printScores() if not durationOK: onError(46, "Durations does not match") printInfo2("Trying again") else: onError(31, "Failed. Video file does not exist") printInfo2("Trying again...") reDownload = True else: videoComment = dlCommentExist break if subCmd: trys = 0 oldReDownload = reDownload while True: trys += 1 if trys > maxTrys: onError(32, "Tried to download subtitles %s times\nSkipping..." % (trys - 1)) subComment = dlCommentError break print printInfo2("Downloading subtitles %s.srt ..." % line['name'].rstrip()) if verbose: printInfo1("from %s" % line['subs']) printInfo1("%s%s try" % (trys, numbering(trys, verbose))) printScores() if continueWithProcess(line['name'].rstrip(), "srt", keepOld, reDownload, "Will redownload\n", "Keeping old file. No download\n", verbose): if verbose: printInfo2("Downloading file...") print line['subs'] print videoCmd result = downloadFile(line['subs'], "%s.%s" % (line['name'].rstrip(), "srt"), verbose) if not result: printScores() onError(33, "Failed to download subtitles") printInfo2("Trying again...") reDownload = True else: fileSizeOK = checkFileSize(line, verbose) if os.path.isfile("%s.srt" % line['name'].rstrip()) and fileSizeOK: printScores() printInfo1("Finished downloading subtitles") setPerms("%s.srt" % line['name'].rstrip(), verbose) reDownload = oldReDownload subComment = dlCommentSuccess break else: printScores() if not fileSizeOK: onError(47, "Failed. File sizes does not match") printInfo2("Trying again") else: onError(34, "Failed. Subtitle file does not exist") printInfo2("Trying again") reDownload = True else: subComment = dlCommentExist break else: subComment = dlCommentNoSub printInfo2("\nGetting file info...") fileSize = getInfo(line, '--Inform="General;%FileSize%"', verbose) fileSizeMeasure = getInfo(line, '--Inform="General;%FileSize/String%"', verbose) duration = getInfo(line, '--Inform="General;%Duration%"', verbose) durationFormatted = getInfo(line, '--Inform="General;%Duration/String3%"', verbose) overallBitRate = getInfo(line, '--Inform="General;%OverallBitRate%"', verbose) overallBitRateMeasure = getInfo(line, '--Inform="General;%OverallBitRate/String%"', verbose) videoFormat = getInfo(line, '--Inform="Video;%Format%"', verbose) videoCodecId = getInfo(line, '--Inform="Video;%CodecID%"', verbose) videoBitRate = getInfo(line, '--Inform="Video;%BitRate%"', verbose) videoBitRateMeasure = getInfo(line, '--Inform="Video;%BitRate/String%"', verbose) width = getInfo(line, '--Inform="Video;%Width%"', verbose) height = getInfo(line, '--Inform="Video;%Height%"', verbose) frameRate = getInfo(line, '--Inform="Video;%FrameRate%"', verbose) frameCount = getInfo(line, '--Inform="Video;%FrameCount%"', verbose) audioFormat = getInfo(line, '--Inform="Audio;%Format%"', verbose) audioCodecId = getInfo(line, '--Inform="Audio;%CodecID%"', verbose) audioBitRate = getInfo(line, '--Inform="Audio;%BitRate%"', verbose) audioBitRateMeasure = getInfo(line, '--Inform="Audio;%BitRate/String%"', verbose) if line['subs']: subSize = os.path.getsize("%s.srt" % line['name'].rstrip()) with open("%s.srt" % line['name'].rstrip()) as myfile: subLines = sum(1 for line in myfile) # number of lines in file myfile.close() # close file else: subSize = "na" subLines = "na" infoDownloaded.append({'videoName': "%s.%s" % (line['name'].rstrip(), line['suffix']), 'expectedDuration': line['duration'], 'videoDlComment': videoComment, 'fileSize': fileSize, 'fileSizeMeasure': fileSizeMeasure, 'duration': duration, 'durationFormatted': durationFormatted, 'overallBitRate': overallBitRate, 'overallBitRateMeasure': overallBitRateMeasure, 'videoFormat': videoFormat, 'videoCodecId': videoCodecId, 'videoBitRate': videoBitRate, 'videoBitRateMeasure': videoBitRateMeasure, 'width': width, 'height': height, 'frameRate': frameRate, 'frameCount': frameCount, 'audioFormat': audioFormat, 'audioCodecId': audioCodecId, 'audioBitRate': audioBitRate, 'audioBitRateMeasure': audioBitRateMeasure, 'subName': "%s.srt" % line['name'].rstrip(), 'expectedSubSize': line['subSize'], 'subDlComment': subComment, 'subSize': subSize, 'subLines': subLines}) printScores() print return infoDownloaded