Guest User

Untitled

a guest
Jul 16th, 2024
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.69 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import os
  3. import subprocess
  4. from sys import argv, stderr
  5.  
  6. def main():
  7.  
  8.     if len(argv) < 2 or "-help" in argv or "--help" in argv or "-h" in argv:
  9.         displayHelp()
  10.         return
  11.  
  12.     # find input file
  13.     try:
  14.         inputIndex = argv.index("-i")
  15.         inputFile = argv[inputIndex + 1]
  16.     except ValueError:
  17.         print("Fatal: no input file given.", file=stderr)
  18.         return
  19.  
  20.     # find the start parameter
  21.     try:
  22.         startIndex = argv.index("-ss")
  23.         startTimestamp = argv[startIndex + 1]
  24.     except ValueError:
  25.         startTimestamp = "0"
  26.  
  27.     # find the t (length) parameter
  28.     try:
  29.         lengthIndex = argv.index("-t")
  30.         lengthTimestamp = argv[lengthIndex + 1]
  31.     except ValueError:
  32.         lengthTimestamp = None
  33.  
  34.     # find the to parameter
  35.     try:
  36.         toIndex = argv.index("-to")
  37.         toTimestamp = argv[toIndex + 1]
  38.     except ValueError:
  39.         toTimestamp = None
  40.  
  41.     # need the duration for size calculations
  42.     if lengthTimestamp is None and toTimestamp is None:
  43.         toEnd = True
  44.  
  45.         # find the length of the file
  46.         command = ["ffprobe", "-i", inputFile, "-show_entries", "format=duration", "-v", "error",
  47.             "-of", "csv=p=0"]
  48.         result = subprocess.run(command, capture_output=True, check=True)
  49.         duration = float(result.stdout) - float(startTimestamp)
  50.     else:
  51.         toEnd = False
  52.         if lengthTimestamp is not None:
  53.             duration = float(lengthTimestamp)
  54.         else:
  55.             duration = float(toTimestamp) - float(startTimestamp)
  56.  
  57.     outputFolder = not "-no_output_folder" in argv
  58.  
  59.     # find special filter flag
  60.     # TODO: implement
  61.  
  62.     # build command
  63.     command = ["ffmpeg", "-hide_banner", "-ss", startTimestamp]
  64.     if not toEnd:
  65.         if lengthTimestamp is not None:
  66.             command.extend(["-t", lengthTimestamp])
  67.         else:
  68.             command.extend(["-to", toTimestamp])
  69.  
  70.     command.extend(["-i", inputFile, "-i", os.path.join("filters", "fortnite-filter.png"),
  71.         "-/filter_complex", os.path.join("filters", "fortnite-filter-540.txt"),
  72.         "-an", "-c:v", "libvpx-vp9", "-b:v", str(3.9 / duration) + "MiB",
  73.         "-pass", "1", "-f", "null", os.devnull])
  74.  
  75.     subprocess.run(command, check=True)
  76.  
  77.     # remove the first pass stuff
  78.     command = command[:-4]
  79.     command.append("2")
  80.     outputFile = inputFile[:inputFile.rfind(".")] + ".webm"
  81.  
  82.     if outputFolder:
  83.         command.append(os.path.join("output", outputFile))
  84.     else:
  85.         command.append(outputFile)
  86.  
  87.     subprocess.run(command, check=True)
  88.  
  89. def displayHelp():
  90.     print("help")
  91.  
  92. if __name__ == "__main__":
  93.     main()
  94.  
Advertisement
Add Comment
Please, Sign In to add comment