Advertisement
shisht

ffplayer

May 17th, 2014
542
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.34 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # file name = ffplayer ( build using ffmpeg multimedia library )
  3. # developer :: Shishtpal
  4.  
  5. import os, glob, shutil, sys, tempfile, subprocess, ctypes, time
  6. import Tkinter as tk
  7. import tkFileDialog
  8.  
  9. vlist = set()
  10. index_no = 0
  11.  
  12. # < Program Code #10001@ >
  13. # Code Block used to add files to vlist, that we use as an argument with this script.
  14. try:
  15.     if len(sys.argv) == 1:
  16.         cwd = ''
  17.     else:
  18.         cwd = sys.argv[1:]
  19.     for i in cwd:
  20.         print i
  21.         vlist.add(i)
  22.     print "\n\n"
  23. except:
  24.     print "Error in Program Code #10001@"
  25.  
  26.  
  27. def tk_gui():
  28.     global root, cwd
  29.     global  index_no
  30.    
  31.     root = tk.Tk()
  32.     root.wm_geometry(newGeometry='250x0+10+10')  
  33.     root.wm_title("Ffplayer ( Video Player )")
  34.  
  35.     menubar = tk.Menu(root)
  36.     root.config(menu=menubar)
  37.     # Creating a menu button
  38.     filemenu1 = tk.Menu(menubar)
  39.     filemenu2 = tk.Menu(menubar)
  40.     filemenu3 = tk.Menu(menubar)
  41.     menubar.add_cascade(label='Player', menu=filemenu1)
  42.     menubar.add_cascade(label='Control', menu=filemenu2)
  43.     menubar.add_cascade(label='Help', menu=filemenu3)
  44.    
  45.     def button1Click():
  46.         """
  47.         this code block is used to play a single video file,
  48.         this video file will be added to vlist playlist
  49.         """
  50.         global cwd, vlist
  51.         print "Opening Video File :-)\n\n"
  52.         openfilename = tkFileDialog.askopenfilename(title="Select Video/Mp3 File..", initialdir="C:\\")
  53.         cwd = openfilename
  54.         vlist.add(cwd)
  55.         vid_player(cwd)
  56.  
  57.     def button2Click():
  58.         """
  59.         this code block is used to open a window dialog box asked you to select
  60.         a folder and filter all video files in your defined location,
  61.         and add all of them to your vlist
  62.         """
  63.         global cwd
  64.         print "Open a folder with Video File :-)\n\n"
  65.         openfoldername = tkFileDialog.askdirectory()
  66.        
  67.         b = openfoldername.split("/")
  68.         cwd = ''
  69.         for i in range(len(b)):
  70.             cwd = cwd + "\\" + b[i]
  71.         cwd = cwd[1:]
  72.         print cwd
  73.         vid_player(cwd)
  74.         play_video()
  75.        
  76.  
  77.     """        
  78.     def button3Click():
  79.         print "Open Settings Panel :-)\n\n\n"
  80.         #p1 = subproecess.call([])
  81.     """
  82.    
  83.     def button4Click():
  84.         print "Bye Bye!!"
  85.         sys.exit()
  86.  
  87.     # create action for button 'Quit'
  88.     filemenu1.add_command(label='Open File (o)', command=button1Click)
  89.     filemenu1.add_command(label='Open Folder (f)', command=button2Click)
  90.     #filemenu1.add_command(label='Settings', command=button3Click) 
  91.     filemenu1.add_separator()
  92.     filemenu1.add_command(label='Quit (q)', command=button4Click) # bind a event using command method
  93.    
  94.     #
  95.     root.bind("<o>", lambda e: button1Click()) # bind a shortcut using lambda method
  96.     root.bind("<O>", lambda e: button1Click())
  97.    
  98.     root.bind("<f>", lambda e: button2Click())
  99.     root.bind("<F>", lambda e: button2Click())
  100.    
  101.     root.bind("<Q>", lambda e: button4Click())
  102.     root.bind("<q>", lambda e: button4Click())
  103.  
  104.     #
  105.     def next_video():
  106.         global p1
  107.         try:
  108.             p1.kill()
  109.         except:
  110.             print "\n\nThere is NO ffplay.exe subprocess Exists!!\n\n"
  111.         play_video()
  112.        
  113.     def play_video():
  114.         global index_no, p1
  115.         print "\n", "##"*35
  116.         print "vlist contents :: ", len(vlist)
  117.         print "    # Current Video file Index Number Is :: ", index_no, "\n"
  118.  
  119.         if len(vlist) > 0:
  120.             try:
  121.                 print "##"*35, "\n\n"
  122.                 p1 = subprocess.Popen(["ffplay.exe", "-autoexit", list(vlist)[index_no]])
  123.             except:
  124.                 pass
  125.         else:
  126.             print "\n\nHello, There is no Video File in your PlayList !!!\n\n\n"
  127.  
  128.         if index_no <= len(vlist):
  129.            
  130.             index_no += 1
  131.  
  132.     def read_manual():
  133.         try:
  134.             subprocess.call(["notepad.exe", "source\\ffplay.help.txt"])
  135.         except:
  136.             print "Manual for ffplay Not Exists!!!"
  137.            
  138.     def start_ffmpeg_web():
  139.         root1 = tk.Tk()
  140.         myContainer2 = tk.Frame(root1)
  141.         myContainer2.pack()
  142.         myLabel1 = tk.Label(myContainer2)
  143.         myLabel1.configure(text="Go to http://trac.ffmpeg.org", background="yellow")
  144.         myLabel1.pack()
  145.         button1 = tk.Button(myContainer2)
  146.         button1.configure(text="OK", background="green")
  147.         button1.pack()
  148.         root1.update()
  149.         time.sleep(2)
  150.         root1.destroy()
  151.    
  152.     def about_ffplayer():
  153.         print "\n  FFPLAYER is a video player build using ffmpeg library..\n\t Build By ( Shishtpal )"
  154.         root1 = tk.Tk()
  155.         myContainer2 = tk.Frame(root1)
  156.         myContainer2.pack()
  157.         myLabel1 = tk.Label(myContainer2)
  158.         myLabel1.configure(text="\n  FFPLAYER is a video player build using ffmpeg library..\n\t Build By ( Shishtpal )", background="cyan")
  159.         myLabel1.pack()
  160.         button1 = tk.Button(myContainer2)
  161.         button1.configure(text="OK", background="green")
  162.         button1.pack()
  163.         root1.update()
  164.         time.sleep(4)
  165.         root1.destroy()
  166.  
  167.        
  168.     def explore_video_folder():
  169.         loop_player()
  170.         global index_no, vlist, cwd, p1
  171.         print "\n##"*10
  172.         print "\n    ## After Exploring Video Folder I Found", len(vlist), "Files.."
  173.         print "    # Current Video file Index Number Is :: ", index_no
  174.         print "    # Your Working Directory is ::", cwd, "\n\n"
  175.        
  176.         if len(vlist) > 0:
  177.             index_no = 0
  178.             if p1.poll() == None:
  179.                 p1.kill()
  180.             if True:
  181.                 for i in range(len(vlist)):
  182.                     print "\n", "##"*35
  183.                     print "\n   #Playing Video File :: ", list(vlist)[index_no], "\n"
  184.                     p1 = subprocess.Popen(["ffplay.exe", "-autoexit", list(vlist)[index_no]])
  185.                     print "\n    # Current Video file Index Number Is :: ", index_no, "\n"
  186.                     print "\n", "##"*35
  187.                     index_no += 1
  188.                     p1.wait()
  189.             else:
  190.                 pass
  191.                
  192.     def loop_video():
  193.         global index_no, p1, vlist
  194.         try:
  195.            
  196.             if p1.poll() == None:
  197.                 p1.kill()
  198.                 print "\n", "##"*35
  199.                 print "\n    # Current Video file Index Number Is :: ", index_no-1, "\n\n"
  200.                 p1 = subprocess.Popen(["ffplay.exe", "-autoexit", list(vlist)[index_no-1]])
  201.             elif len(vlist) == 1:
  202.                 print "\n", "##"*35
  203.                 print "\n    # Current Video file Index Number Is :: ", index_no, "\n\n"
  204.                 p1 = subprocess.Popen(["ffplay.exe", "-autoexit", list(vlist)[index_no]])
  205.             else:
  206.                 print "\n", "##"*35
  207.                 print "\n    # Current Video file Index Number Is :: ", index_no-1, "\n\n"
  208.                 p1 = subprocess.Popen(["ffplay.exe", "-autoexit", list(vlist)[index_no-1]])
  209.            
  210.         except:
  211.             print "  # Error in loop_video Function. #"
  212.    
  213.     def previous_video():
  214.         global index_no, p1, vlist
  215.         if index_no > 1:
  216.             try:
  217.                 if p1.poll() == None:
  218.                     p1.kill()
  219.                     p1 = subprocess.Popen(["ffplay.exe", "-autoexit", list(vlist)[index_no-2]])
  220.                    
  221.                 else:
  222.                     p1 = subprocess.Popen(["ffplay.exe", "-autoexit", list(vlist)[index_no-2]])
  223.                 print "\n    # Current Video file Index Number Is :: ", index_no-2, "\n"
  224.                 print "##"*35, "\n"
  225.                
  226.                 if index_no != 0:
  227.                     index_no -= 1
  228.             except:
  229.                 print "Error While playing Previous Video File.."
  230.         else:
  231.             print "There is No Previous Video File Exists..."
  232.             index_no = 0
  233.        
  234.     root.bind("<m>", lambda e: play_video())
  235.     root.bind("<M>", lambda e: play_video())
  236.    
  237.     root.bind("<n>", lambda e: next_video())
  238.     root.bind("<N>", lambda e: next_video())
  239.    
  240.     root.bind("<k>", lambda e: previous_video())
  241.     root.bind("<K>", lambda e: previous_video())
  242.    
  243.     root.bind("<l>", lambda e: loop_video())
  244.     root.bind("<L>", lambda e: loop_video())
  245.    
  246.     filemenu2.add_command(label='Play Video (m)', command=play_video)
  247.     filemenu2.add_command(label='Next Video (n)', command=next_video)
  248.     filemenu2.add_command(label='Previous Video (k)', command=previous_video)
  249.     filemenu2.add_command(label='Loop Video (l)', command=loop_video)
  250.     filemenu2.add_separator()
  251.    
  252.     def print_playlist():
  253.         print "\n", "@#"*30
  254.         print "Play-list Contents :: ", "\n"
  255.         for i, j  in enumerate(vlist, 1):
  256.             print i, j
  257.         print "@#"*30, "\n"
  258.     filemenu2.add_command(label='Print Playlist', command=print_playlist)
  259.    
  260.     def open_playlist_file():
  261.         global vlist, index_no, p1
  262.         print "Opening Play-list File :: "
  263.         try:
  264.             openfilename = tkFileDialog.askopenfilename(filetypes=[("PLT", "*.plt" )], title="Select Your Playlist File..", initialdir="C:\\")
  265.             print "\n", "@#"*30
  266.             print "You Select :: \n(", openfilename, ") \n\tPlay-list File..", "\n"
  267.             y = open(openfilename)
  268.             while True:
  269.                 a = y.readline()[:-1]
  270.                 if len(a) == 0:
  271.                     break
  272.                 else:
  273.                     vlist.add(a)
  274.             y.close()
  275.             print "\n", "@#"*30, "\n"
  276.         except:
  277.             print "No Play-List has been Selected..\n"
  278.     filemenu2.add_command(label='Open Playlist File', command=open_playlist_file)
  279.    
  280.     def write_playlist_file():
  281.         global vlist
  282.         print "\n", "**"*30
  283.         print "We have Write your Current Play-List File To Disk.."
  284.         print "ffplayer_current_playlist.plt", "\n"
  285.         x = open("ffplayer_current_playlist.plt", "w")
  286.         for i in vlist:
  287.             w = i+"\n"
  288.             x.write(w)
  289.         x.close()
  290.         print "**"*30, "\n"
  291.     filemenu2.add_command(label='Write Playlist File', command=write_playlist_file)
  292.    
  293.     def custom_playlist():
  294.         print "Customizing Play-list Contents :: "
  295.        
  296.     #filemenu2.add_command(label='Custom Playlist', command=custom_playlist)
  297.     filemenu2.add_separator()
  298.     filemenu2.add_command(label='Explore Video Folder', command=explore_video_folder)
  299.    
  300.     filemenu3.add_command(label='Manual', command=read_manual)
  301.     filemenu3.add_command(label='About FFMPEG', command=start_ffmpeg_web)
  302.    
  303.     def ffmpeg_howto():
  304.         try:
  305.             subprocess.Popen(["notepad.exe", "source\\ffmpeg.howto.txt"])
  306.         except:
  307.             print "FFMPEG How To Documentation File Not Exists.."
  308.     filemenu3.add_command(label='FFMPEG How TO ', command=ffmpeg_howto)
  309.     filemenu3.add_command(label='About ffplayer', command=about_ffplayer)
  310.  
  311.  
  312. def screen_resolution():
  313.     """
  314.     this code block is used to get Screen resolution of your desktop Screen.   
  315.     """
  316.     global screensize
  317.     user32 = ctypes.windll.user32
  318.     screensize = user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)
  319.  
  320.     print "Screen Resolution is :: ", screensize, "\n\n"
  321. screen_resolution()
  322.  
  323. def vid_filter(filename):
  324.     if os.path.isdir(filename) | filename.endswith(".jpg") | \
  325.         filename.endswith(".png") | filename.endswith(".jpeg") | \
  326.         filename.endswith(".JPG") | filename.endswith(".PNG") | \
  327.         filename.endswith(".JPEG") | filename.endswith(".txt") | \
  328.         filename.endswith(".tar.gz") | filename.endswith(".zip") | \
  329.         filename.endswith(".7z") | filename.endswith(".exe") | \
  330.         filename.endswith(".pdf") | filename.endswith(".htm") | \
  331.         filename.endswith(".html") | filename.endswith(".mhtm") | \
  332.         filename.endswith(".mhtml") | filename.endswith(".lnk") | \
  333.         filename.endswith(".mht") | filename.endswith(".ini") | \
  334.         filename.endswith(".db") | filename.endswith(".img") | \
  335.         filename.endswith(".iso") | filename.endswith(".pkg") | \
  336.         filename.endswith(".sh") | filename.endswith(".tgz") | \
  337.         filename.endswith(".css") | filename.endswith(".gif") | \
  338.         filename.endswith(".c4d") | filename.endswith(".xml") | \
  339.         filename.endswith(".apk") | filename.endswith(".dat") | \
  340.         filename.endswith(".doc") | filename.endswith(".docx") | \
  341.         filename.endswith(".ppt") | filename.endswith(".pptx") | \
  342.         filename.endswith(".xls") | filename.endswith(".xlsx") | \
  343.         filename.endswith(".py") | filename.endswith(".pyw") | \
  344.         filename.endswith(".pyc") | filename.endswith(".pyo") | \
  345.         filename.endswith(".chm") | filename.endswith(".js")  | \
  346.         filename.endswith(".gz") | filename.endswith(".cfg")  | \
  347.         filename.endswith(".inf") | filename.endswith(".sys")  | \
  348.         filename.endswith(".dll"):
  349.         return True
  350.  
  351.  
  352. def vid_player(cwd):
  353.     global p1, vlist
  354.     if os.path.isfile(cwd):
  355.         vlist.add(cwd)
  356.         p1 = subprocess.Popen(["ffplay.exe", "-autoexit", cwd])
  357.        
  358.     elif os.path.isdir(cwd):
  359.         for i in os.listdir(cwd):
  360.             try:
  361.                 filename = os.path.join(cwd, i)
  362.                 if vid_filter(filename):
  363.                     pass
  364.                 else:
  365.                     vlist.add(filename)
  366.             except:
  367.                 print "Error While Filtering File Formats..\n\t In vid_player Function..\n"
  368.                 pass
  369.                
  370.     else:
  371.         print "Hi, I Do not Know, what you Defined !!\n\t as a video file or a folder which contains video files..\n"
  372.  
  373. def loop_player():
  374.     """
  375.     This code Block create a list of each and every file recursively,
  376.     But we have used a filter So that wrong File Formats will not be used with
  377.     ffplayer, because this just gives you an error,
  378.     we can also use ffrpobe to know if supplied file is a video file, but this is just
  379.     time consuming process, this would be great if you just use a filter.
  380.     """
  381.     global files, vlist, index_no, cwd
  382.     files = []  
  383.     for i in zip(os.walk(cwd)):
  384.         files.append(i)
  385.  
  386.     for i in range(len(files)):
  387.         for j in files[i][0][2]:
  388.             try:
  389.                 filename = os.path.join(files[i][0][0], str(j))
  390.  
  391.                 if vid_filter(filename):
  392.                     pass
  393.                 else:
  394.                     vlist.add(filename)
  395.  
  396.             except:
  397.                 print "Error While Filtering File Formats !! In explore video folder Function !!"
  398.                 pass
  399.  
  400.  
  401. #
  402. tk_gui()
  403.  
  404. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement