Advertisement
Guest User

Copy file ffmpeg

a guest
Mar 27th, 2018
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.66 KB | None | 0 0
  1. #!/usr/bin/python
  2. import sys, os, subprocess, shlex, re, json, requests
  3. from subprocess import call
  4.  
  5. # FFprobe function
  6. def FFprobe(filename):
  7.     cmnd = ['ffprobe', '-show_streams', '-print_format', 'json', filename]
  8.     p = subprocess.Popen(cmnd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  9.     out, err =  p.communicate()
  10.     return out
  11.  
  12. # Check arguments
  13. if len(sys.argv) != 3:
  14.     print "Invalid arguments. Give 2 inputs"
  15.     print "  Format: python create_bumper.py <input> <output>"
  16.     quit()
  17.  
  18. # Defining variables
  19. inputFile = sys.argv[1]
  20. outputFile = sys.argv[2]
  21.  
  22. # Probing
  23. probe= FFprobe(inputFile)
  24.  
  25. # Parsing JSON
  26. json = json.loads(probe)
  27.  
  28. # Looping each stream
  29. for stream in json["streams"]:
  30.     if stream["codec_type"] == "video":
  31.         videoIndex = stream["index"]
  32.     elif stream["codec_type"] == "audio":
  33.         audioIndex = stream["index"]
  34.  
  35. video = json["streams"][videoIndex]
  36. audio = json["streams"][audioIndex]
  37.  
  38. # Creating arguments for ffmpeg
  39. executable = ['ffmpeg', '-hide_banner', '-y', '-v', 'error', '-i', inputFile]
  40. videoCommands = ['-c:v', 'libx264', '-profile:v', video["profile"], '-level', str(video["level"]), '-pix_fmt', video["pix_fmt"], '-filter_complex', 'scale='+str(video["width"])+':'+str(video["height"]), '-bf', '0' ]
  41. audioCommands = ['-c:a', 'aac', '-ar', audio["sample_rate"]]
  42. outputCommands = [ outputFile ]
  43. cmnd = executable + videoCommands + audioCommands + outputCommands
  44.  
  45. # Running ffmpeg with the gathererd data
  46. p = subprocess.Popen(cmnd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  47. out, err =  p.communicate()
  48.  
  49. if err:
  50.     print err
  51.     print cmnd
  52. else:
  53.     print " >> Converted succesfully!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement