Advertisement
shisht

ffconverter

May 17th, 2014
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.02 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # filename :: video converter using Python ( Based Upon FFMPEG Library )
  3.  
  4. print "##"*35
  5. print "Video Converter Build using Python and FFMPEG library.."
  6.  
  7. import os
  8. import sys
  9. import tempfile
  10. import glob
  11. import subprocess
  12. import Tkinter as tk
  13.  
  14. cwd = ''        # Input file to convert
  15. cwd21a = ''     # Output file to Convert
  16. vlist = set()   # Used to create a playlist for converting multiple files simultaneously
  17. format = ''     # file format or extension to which you want to convert
  18. duration = ''
  19. size_of_video = ''
  20. frame_per_sec = ''
  21. z = 0
  22. ffmpeg_command = ''
  23.  
  24. def tk_gui():
  25.     root = tk.Tk()
  26.     #root.wm_geometry(newGeometry='250x250+10+10')  
  27.     root.wm_title("Ffconverter ( Video Converter )")
  28.    
  29.     # Container Number 1
  30.     myContainer1 = tk.Frame(root)
  31.     myContainer1.pack()
  32.    
  33.     myLabel1 = tk.Label(myContainer1)
  34.     myLabel1.configure(text="  Select File To Convert  ::   ", background="yellow")
  35.     myLabel1.pack(side=tk.LEFT)
  36.    
  37.     entry1 = tk.Entry(myContainer1)
  38.     entry1.configure(width=100)
  39.     entry1.pack(side=tk.LEFT)
  40.    
  41.     def select_file():
  42.         """
  43.         this code block is used to play a single video file,
  44.         this video file will be added to vlist playlist
  45.         """
  46.         global cwd, vlist
  47.         print "Opening Video File :-)\n\n"
  48.         openfilename = tkFileDialog.askopenfilename()
  49.         cwd = openfilename
  50.         print "You Have Select :: ", cwd
  51.         entry1.insert(0, cwd)
  52.         root.update()
  53.  
  54.     button1 = tk.Button(myContainer1)
  55.     button1.configure(text="Select", background="green", command=select_file)
  56.     button1.pack(side=tk.LEFT)
  57.    
  58.     ## Container Number 3
  59.     myContainer3 = tk.Frame(root)
  60.     myContainer3.pack()
  61.    
  62.     myLabel3 = tk.Label(myContainer3)
  63.     myLabel3.configure(text="Enter Format Name to Convert ::", background="yellow")
  64.    
  65.     myLabel3.pack(side=tk.LEFT)
  66.  
  67.     entry3 = tk.Entry(myContainer3)
  68.     entry3.configure(width=20)
  69.     entry3.pack(side=tk.LEFT)
  70.    
  71.     def get_format():
  72.         global format
  73.         format = tk.StringVar()
  74.         print "Retrieving File Format.."
  75.         entry3.index(0)
  76.         format = entry3.get()
  77.         print "You want to Convert to :: ", format
  78.         print "##"*40, "\n"
  79.  
  80.        
  81.     button3 = tk.Button(myContainer3)
  82.     button3.configure(text="OK  ", background="cyan", command=get_format)
  83.     button3.pack(side=tk.LEFT)
  84.     ##
  85.     myLabel5 = tk.Label(myContainer3)
  86.     myLabel5.configure(text="Duration, Frame Size, Frame Rate ", background="yellow")
  87.     myLabel5.pack(side=tk.LEFT)
  88.  
  89.     entry4 = tk.Entry(myContainer3)
  90.     entry4.configure(width=50)
  91.     entry4.pack(side=tk.LEFT)
  92.  
  93.     def get_size():
  94.         global cwd, duration, size_of_video, frame_per_sec
  95.         print "Checking for Oroginal Size of Video File.."
  96.         filename = cwd
  97.         result = subprocess.Popen(["ffprobe.exe", filename], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  98.         duration0 = [x for x in result.stdout.readlines() if "Duration" in x]
  99.         duration = duration0[0].split()[1]
  100.         result = subprocess.Popen(["ffprobe.exe", filename], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  101.         size_of_video0 = [x for x in result.stdout.readlines() if "Video:" in x]
  102.         size_of_video1 = size_of_video0[0].split()
  103.         for i in size_of_video1:
  104.             if "x" in i:
  105.                 size_of_video = i
  106.         frame_per_sec0 = size_of_video0[0].split()
  107.         print frame_per_sec0
  108.         frame_per_sec1 = frame_per_sec0.index("fps,")
  109.         frame_per_sec = frame_per_sec0[frame_per_sec1 - 1]
  110.        
  111.         print "\n", "##"*35
  112.         print "Duration of Video File :: ", duration
  113.         print "Size of Video Frame :: ", size_of_video
  114.         print "Frame Rate of Video :: ", frame_per_sec
  115.         print "\n", "##"*35, "\n"
  116.         contents = duration + " " + size_of_video + " " +frame_per_sec
  117.         entry4.insert(0, contents)
  118.        
  119.     button5 = tk.Button(myContainer3)
  120.     button5.configure(text="OK  ", background="cyan", command=get_size)
  121.     button5.pack(side=tk.LEFT)
  122.    
  123.     ## Conatiner Number 2
  124.     myContainer2 = tk.Frame(root)
  125.     myContainer2.pack()
  126.  
  127.  
  128.     myLabel2 = tk.Label(myContainer2)
  129.     myLabel2.configure(text=" File Converted to     ::      ", background="yellow")
  130.     myLabel2.pack(side=tk.LEFT)
  131.    
  132.    
  133.     entry2 = tk.Entry(myContainer2)
  134.     entry2.configure(width=100)
  135.     entry2.pack(side=tk.LEFT)
  136.    
  137.     def Converted_file():
  138.         global cwd, vlist, format, cwd21a
  139.         cwd1 = os.path.split(cwd)[0]
  140.         cwd2 = os.path.split(cwd)[1]
  141.         cwd21 = cwd2[:-4]
  142.         cwd21a = cwd1 + cwd21 + "__converted__." + str(format)
  143.         print "File will be Converted to :: ", cwd21a
  144.         entry2.index(0)
  145.         entry2.insert(0, cwd21a)
  146.        
  147.         text1.index(1.0)
  148.         ffmpeg_command_1 = "ffmpeg.exe" + " -i" + " \""+str(cwd)+"\"" + " \""+str(cwd21a)+"\""+"\n"
  149.         text1.insert(1.0, ffmpeg_command_1)
  150.        
  151.        
  152.     button2 = tk.Button(myContainer2)
  153.     button2.configure(text="Save  ", background="green", command=Converted_file)
  154.     button2.pack(side=tk.LEFT)
  155.    
  156.     button7 = tk.Button(myContainer2)
  157.     def clear_entry():
  158.         entry1.delete(0, tk.END)
  159.         entry2.delete(0, tk.END)
  160.         entry3.delete(0, tk.END)
  161.         entry4.delete(0, tk.END)
  162.        
  163.     button7.configure(text="Clear ", background="red", command=clear_entry)
  164.     button7.pack(side=tk.LEFT)
  165.    
  166.     ## Container Number 4
  167.     myContainer4 = tk.Frame(root)
  168.     myContainer4.pack()
  169.    
  170.     #myLabel4 = tk.Label(myContainer4)
  171.     #myLabel4.configure(text="Run Custom FFMPEG Commands", background="red")
  172.     #myLabel4.pack(side=tk.LEFT)
  173.  
  174.     text1 = tk.Text(myContainer4)
  175.     text1.configure(width=100)
  176.     text1.pack(side=tk.LEFT)
  177.    
  178.     def get_ffmpeg_command():
  179.         global format, cwd, cwd21a, z, ffmpeg_command
  180.         format = tk.StringVar()
  181.         print "Retrieving File Format.."
  182.         text1.index(1.0)
  183.         #z = 0
  184.        
  185.         try:
  186.             start = str(z+1)+"."+str(0)
  187.             end = str(z+1)+"."+str("end")
  188.             try:
  189.                 if len(text1.get(start, end)) == 0:
  190.                     #break
  191.                     print "There is No Custom FFMPEG Commands Exists in This Line..!!"
  192.                     print "Value of video Index Number z == ", z
  193.                     pass
  194.                 else:
  195.                     z += 1
  196.                     ffmpeg_command = text1.get(start, end) # one FFMPEG command must be without a new line character
  197.                     print "\n", "@#"*35
  198.                     print "You want to run This FFMPEG Command >> \n", ffmpeg_command
  199.                     print "\n", "@#"*35
  200.                     print "\n\n"
  201.                     os.system(ffmpeg_command)
  202.                     #p1 = subprocess.Popen([ffmpeg_command])
  203.             except:
  204.                 print "No More FFMPEG Custom Commands Found In Text Box.."
  205.                 pass
  206.         except:
  207.             print "Error While Running Custom FFMPEG codes.."
  208.             z += 1
  209.             pass
  210.  
  211.        
  212.     button4 = tk.Button(myContainer4)
  213.     button4.configure(text="Run Custom FFMPEG Command", background="cyan", command=get_ffmpeg_command)
  214.     button4.pack()
  215.    
  216.     button5 = tk.Button(myContainer4)
  217.     def ffmpeg_manual():
  218.         print "Opening FFMPEG Manual .."
  219.         try:
  220.             subprocess.Popen(["notepad.exe", "source\\ffmpeg.help.txt"])
  221.         except:
  222.             print "FFMPEG Manual Not Exists.."
  223.             pass
  224.     button5.configure(text="  Help?  ", background="green", command=ffmpeg_manual)
  225.     button5.pack()
  226.    
  227.     button6 = tk.Button(myContainer4)
  228.     def ffmpeg_examples():
  229.         print "Opening FFMPEG Examples .."
  230.         try:
  231.             subprocess.Popen(["notepad.exe", "source\\ffmpeg.examples.txt"])
  232.         except:
  233.             print "FFMPEG Manual Not Exists.."
  234.             pass
  235.     button6.configure(text="Examples", background="green", command=ffmpeg_examples)
  236.     button6.pack()
  237.    
  238.     ##
  239.  
  240.  
  241.    
  242. tk_gui()
  243.  
  244. while True:
  245.     try:
  246.         exec(raw_input(":: "))
  247.     except:
  248.         pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement