Coders Packet

Trim an Audio file with FFMPEG using Tkinter Python

By Ramya Sri Balla

This project aims to trim an audio file with FFmpeg using Tkinter GUI(Graphical User Interface) Python. FFmpeg is a software, which is a multimedia framework.

First, make sure FFmpeg is installed in your system.

Steps to install ffmpeg for Windows

   

    Here is the download link: https://www.ffmpeg.org/download.html

  • once downloaded the ffmpeg we need to open that file then choose extract file on right-click, then save it to some location.
  • Add this folder to the windows path, to run it, just come to the search icon and search env and click the Environment variables button. 
  • select the path variable under User variables and click Edit. 
  • Add the FFmpeg directory to the path and click ok to finish. 

FFmpeg

   FFmpeg is software that is a multimedia framework that handles audio and video files and other multimedia files. ffmpeg is a very fast video and audio converter. Using ffmpeg we can manipulate all multimedia files. It is used to convert files, trim video and audio files, reduce the file size and crop the video, etc.

   Use this command to install ffmpeg.

      pip install ffmpeg

ListBox

     A list box is a box that holds a list of items. It's a tkinter widget that can be used for all sorts of things. We can add, select and delete the items.

   here we use Listbox curselection to display the selected items. curselection is a predefined function that fetches the values of a selected items.

Glob module

    Here we use glob which is an inbuilt module in the Python standard library, which helps you to find the file pathnames that match a specific pattern.

 

Tkinter is the inbuilt library of Python that allows you to create a GUI Graphical User Interface, a relatively easy and most commonly used application.

let us create a Tkinter application 

Creation of Tkinter application  

  1. Import the Tkinter module

  2. Create the GUI application main window

  3. Add widgets to the main window

  4. Apply the event Trigger on the widgets.

Here is the code implementation:

 

from tkinter import *
import os
import glob
root=Tk()
root.geometry("400x530")
root.config(bg="black")
label=Label(root,text="TRIM AN AUDIO FILE",font=("times new roman",25,"bold"),fg="white",bg="black")
label.place(x=27,y=50)
label=Label(root,text="select an audio from list",font=("times new roman",13,"bold"),fg="blue",bg="black")
label.place(x=40,y=115)
#function for adding audio to entry field
def add_to_entry(event):
    global item
    n=lst.curselection()
    item=lst.get(n)
    v.set(item)
#function for trim an audio
def trims():
    short=item[:-4]+"new.mp3"
    start=input("enter start in seconds:")  
    end=input("enter end in seconds:") 
    command=f"ffmpeg -i {item} -ss {start} -to {end} -c copy {short}"
    os.system(command)
#function for exit an application
def exit():
    root.destroy()
    
#creation of list box
lst=Listbox(root)
lst.place(x=150,y=150)
for f in glob.glob("*.mp3"):
    lst.insert("end",f)
lst.bind("",add_to_entry)


v=StringVar()
entry=Entry(root,textvariable=v,fg="red")
entry.place(x=230,y=120)
#creation of buttons
button1=Button(root,text="TRIM AN AUDIO FILE",fg="blue",font=("times new roman",15,"bold"),command=trims,cursor="hand2")
button1.place(x=100,y=350)
button3=Button(root,text="EXIT APPLICATION",fg="red",font=("times new roman",15,"bold"),command=exit,cursor="hand2")
button3.place(x=115,y=420)

    
root.mainloop()


 Output:

Download project

Reviews Report

Submitted by Ramya Sri Balla (ballaramyasri4444)

Download packets of source code on Coders Packet