FE 'tkGooies' Utilities'AUDIOtools' group
A Tk GUI ('tkGooie') for Audio
|
GUI interface of the AUDIO EDITOR 'tkGooie' (a Front End for the 'ffmpeg' command)(FE = Freedom Environment) |
FE Home Page >
FE Downloads Page >
FE 'tkGooies' Description Page >
FE 'tkGooies' 'AUDIOtools' Page >
This tkAudioFileEditor (frontend for 'ffmpeg') tkGooie Page
INTRODUCTION to On 2014jun18, I published (at wiki.tcl.tk) a utility for editing MOVIE files using the 'ffmpeg' command. I have reproduced (i.e. backed up) that code --- and description of the code --- at a page on this freedomenv.com site:
I mentioned at the bottom of that page that I planned to create a similar 'ffmpeg'-based 'tkAudioEdit' utility. Such an AUDIO-file-editing utility (a front-end for the 'ffmpeg' command) is the subject of this page. Like the title of this page implies, the main functions of that utility are intended to be
for audio files of the many formats supported by the 'ffmpeg' command. THE GOALS My goals for the Tcl-Tk script for this GUI were similar to those for the 'tkMovieEdit' utility, but without the video options. Namely:
THE GUI LAYOUT Like for the 'movie-edit' utility that I posted here, I made a 'text-sketch' for the GUI for this 'audio-edit' utility. |
CONVENTIONS for the GUI 'text-sketch' below:
* SQUARE-BRACKETS indicate a comment not to be included on the GUI.
* BRACES indicate a Tk 'button' widget.
* UNDERSCORES indicate a Tk 'entry' widget.
* A COLON indicates that the text before the colon is on a 'label' widget.
* CAPITAL-O indicates a Tk 'radiobutton' widget.
* CAPITAL-X indicates a Tk 'checkbutton' widget.
----------------------------------------------------------------------------------------
Audio Edit ( Clip, Change Volume, Convert, Etc. ) - a front-end for the 'ffmpeg' command
[window title]
----------------------------------------------------------------------------------------
[On the left of the following frames will be a Tk 'listbox' widget in frame '.fRleft'.
The following frames will be contained in frame '.fRright'.]
'.fRright'
SubFrame
names
|
V
.fRbuttons {Exit} {Help} {ExecOption} {PlayOutAudio} {PlayInAudio} X MoreAudioOutParms (audio,other)
.fRinfile Input audio filename : $env(HOME)/input_audio.mp3_______________________ {Browse...}
.fRmsg Select an input file and an audio-processing option from the listbox on the left. Then provide
processing parameters at prompts in the 'ExecOption Parameters' section that appears below.
An 'xterm' window will appear when processing starts. You can see processing messages in
that window. When processing is finished, use the 'PlayOutAudio' button to play the audio.
[This initial message in a label widget may be changed when the 'Exec' button is clicked.]
--------------------------------------------------------------------------------------
.fRoutHead Output File parameters:
--------------------------------------------------------------------------------------
.fRencoder Encoding format: O MP3 (.mp3) O Vorbis (.oga) O AAC (.aac) O FLAC (.flac) O Mpeg2 (.mp2)
.fRoutfile Output audio filename : /tmp/$env(USER)_output_audio.mp3___________________
.fRplayer Player for audio in/out file: xffplay____ Examples: xffplay xmplayer gmplayer totem vlc {Help}
--------------------------------------------------------------------------------------
.fRoptsHead ExecOption Parameters: [This label may dynamically change according to option selected.
Examples: ' (CLIP)' or ' (CONVERT)' may be added after 'Parameters:'.]
--------------------------------------------------------------------------------------
.fRexecOpts [The sub-frames and widgets that appear here
will depend on the listbox line currently selected.
An example, for CLIP, is seen in a screenshot below.]
.fRaudparms [This frame and its following subframes are shown if
the 'MoreAudioOutParms' checkbuttons is set ON.]
'.fRaudparms'
SubFrame
names
|
V
-------------------------------------------------------------------------------------
.fRaudioHead Audio-out parameters:
-------------------------------------------------------------------------------------
.fRacodec Audio-out Codec: libmp3lame__ Examples: libmp3lame libvorbis libfaac flac mp2 pcm_s16le
.fRachannels Audio-out Channels: 1_ Examples: 1 2
.fRasrate Audio-out Sampling Rate: 22050__ Examples: 44100 22050 11025
.fRabrate Audio-out Bit Rate: 96k__ Examples: 64k 96k 128k 160k 192k (multiples of 32k)
.fRaother Other audio-out parms: _________________________________________________
--------------------------------------------------------------------------------------
.fRother Other output recording parameters:
--------------------------------------------------------------------------------------
.fRthreads Threads: 1___ (to take advantage of a multi-core computer ; may not work for some coders)
.fRguide NOTE: This utility runs 'ffmpeg' in an 'xterm' window. Startup and coding messages from 'ffmpeg'
can be seen in that window. You may minimize the 'xterm' window while processing, if you do not
want to monitor the processing messages. When processing is done, the terminal does not close,
so that you can examine messages. The output file, if good, can be shown in an audio player,
that you specify above. See 'Help' for details.
[a 'label' widget]
---------------------------------------------------------------------------------------
GUI Components From the GUI 'sketch' above, it is seen that the GUI (not counting the widgets in the 'execOpts' frame) consists of ABOUT:
All but the 'label' widgets provide operating parameters or options in this utility. Hence there are about 7+9+1+5 = 22 options on this utility --- not counting the user-selectable options in the listbox (approximately another 7 options, eventually). And most of those 7 'exec-options' have input parameters. I should point out here that I was not especially interested in coming up with a 'beautiful utility'. I just wanted a utility that would make 'semi-interactive' editing of audio files, with 'ffmpeg', as simple as a few clicks --- on a 'listbox' and an 'Exec button' and a 'Play button' --- and, optionally, entering a few characters in a couple of 'entry' widgets. I am certainly interested in making pretty GUI's --- as my pages on
have indicated. But at this time, I am satisfied to implement the 'functionality', and let the 'beauty' go for a later date (when I have more beauty tools/code at hand). SCREENSHOTS OF THE GUI On the basis of the GUI-layout sketch above, I ended up with the GUI seen in the following image. |
Poke this image to see a full-size version,
shown in a separate browser window or tab.
The basic operating sequence is
You can change some of the initial values on the GUI, like the initial encoding-format, by changing a 'set ENCODERname' statement at the bottom of the Tk script. If the user wants to change (or simply examine) the audio and 'other' parameters that are set according to a choice of the 'encoding format', the user can click on the 'MoreAudioOutParms' checkbutton at the top of the GUI. When that checkbutton is clicked, the GUI expands downward, as can be seen in the following image. |
Poke this image to see a full-size version,
shown in a separate browser window or tab.
When the 'ExecOption' button is clicked and an 'ffmpeg' edit operation starts, an 'xterm' window pops up that shows the messages coming from the 'ffmpeg' program during its operation. THE AUDIO ENCODING FORMATS I tested the 5 encoding-format radiobuttons to confirm that the audio parameters that I have provided for each encoding-format do indeed result in a successful output audio file creation. So I have created '.mp3' , '.oga' , '.aac' , '.flac' , and '.mp2' files. When the user clicks on the 'Help' button of the GUI, there is some information on these formats --- and many web links (URL's) that will give further information. DESCRIPTION OF THE CODE Below, I provide the Tk script code for this 'audio-edit' utility. I follow my usual 'canonical' structure for Tk code for this Tk script: |
0) Set general window and widget parms (win-name, win-position,
win-color-scheme, fonts-for-widgets, widget-geometry-parms,
text-array-for-labels-etc, win-size-control).
1a) Define ALL frames (and sub-frames, if any).
1b) Pack ALL frames and sub-frames.
2) Define and pack all widgets in the frames, frame by frame.
Within each frame, define ALL the widgets.
Then pack the widgets.
3) Define keyboard and mouse/touchpad/touch-sensitive-screen action
BINDINGS, if needed.
4) Define PROCS, if needed.
5) Additional GUI initialization (typically with one or more of
the procs), if needed.
This Tk coding structure is discussed in more detail on the page A Canonical Structure for Tk Code --- and variations. This structure makes it easy for me to find code sections --- while generating and testing a Tk script, and when looking for code snippets to include in other scripts (code re-use). I call your attention to step-zero. One thing that I started doing in 2013 is use of a text-array for text in labels, buttons, and other widgets in the GUI. This can make it easier for people to internationalize my scripts. I will be using a text-array like this in most of my scripts in the future. Experimenting with the GUI As in all my scripts that use the 'pack' geometry manager (which is all of my 100-plus scripts, so far), I provide the four main 'pack' parameters --- '-side', '-anchor', '-fill', '-expand' --- on all of the 'pack' commands for the frames and widgets. That helps me when I am initially testing the behavior of a GUI (the various widgets within it) as I resize the main window. I think that I have used a pretty nice choice of the 'pack' parameters. The label and button and radiobutton widgets stay fixed in size and relative-location if the window is re-sized --- while the input/output filename and 'player-program' entry widgets expand/contract horizontally whenever the window is re-sized horizontally. I decided to make the 'listbox' width fixed and devote most of the window space to the widgets on the right side of the GUI. You can experiment with the '-side', '-anchor', '-fill', and '-expand' parameters on the 'pack' commands for the various frames and widgets --- to get the widget behavior that you want. Additional experimentation: You might want to change the fonts used for the various GUI widgets. For example, you could change '-weight' from 'normal' to 'bold' --- or change '-slant' from 'roman' to 'italic' --- or change the font sizes. Or change font families. In fact, you may NEED to change the font families, because the families I used may not be available on your computer --- and the default font that the 'wish' interpreter chooses may not be very pleasing. I use variables to set geometry parameters of widgets --- parameters such as border-widths and padding. And I have included the '-relief' parameter on the definitions of frames and widgets. Feel free to experiment with those 'appearance' parameters as well. If you find the gray palette of the GUI is not to your liking, you can change the value of the RGB parameter supplied to the 'tk_setPalette' command near the top of the code. Some features in the code There are plenty of comments in the code, to describe what most of the code-sections are doing. You can look at the top of the PROCS section of the code to see a list of the procs used in this script, along with brief descriptions of how they are called and what they do. The main procs are : |
'get_INfilename' - called by the audio in-file 'Browse' button.
'get_chars_before_last' - called by the proc 'get_INfilename'.
'set_defaults_for_encoding' - called by bindings on the ENCODING FORMAT
radiobuttons.
Sets defaults for the audio-and-other
GUI widgets, for a given encoding type.
'loadOptsFrame_perListboxSelection' - called by a click on the listbox.
Calls on a 'show_*_opts' proc depending on
the listbox line (audio-exec-operation) selected.
Example proc name: 'show_CLIP_opts'
'exec_audio_operation' - called by a click on the 'ExecOption' button.
Calls on a 'set_*_cmd' proc depending on
the listbox line (media-file-operation) selected.
Example proc name: 'set_CLIP_cmd'
Then executes the 'ffmpeg' command string built by
the 'set_*_cmd' proc --- in an 'xterm' window.
--------------------------------------------------------------------------------------
For each ExecOption added to the listbox, a 'show_*_opts' proc and a 'set_*_cmd' proc
should be provided --- like the following.
(And calls will need to be added to the 2 procs above:
'loadOptsFrame_perListboxSelection' and 'exec_audio_operation'.)
--------------------------------------------------------------------------------------
'show_CLIP_opts' - called by a button1-release binding on the listbox,
which calls the 'loadOptsFrame_perListboxSelection' proc.
Loads 'clip' frames and widgets into an 'execOpts' frame.
'set_CLIP_cmd' - called by the 'exec_audio_operation' proc which is called
by the 'ExecOption' button.
Sets an 'ffmpeg' command string for CLIPPING --- to be
used in the proc 'exec_audio_operation'.
--------------------------------------------------------------------------------------
'show_CHG-VOLUME_opts' - called by a button1-release binding on the listbox,
which calls the 'loadOptsFrame_perListboxSelection' proc.
Loads 'chg-volume' frames and widgets into an 'execOpts' frame.
'set_CHG-VOLUME_cmd' - called by the 'exec_audio_operation' proc which is called
by the 'ExecOption' button.
Sets an 'ffmpeg' command string for CHG-VOLUME --- to be
used in the proc 'exec_audio_operation'.
--------------------------------------------------------------------------------------
'show_PROPSin_opts' - called by a button1-release binding on the listbox,
which calls the 'loadOptsFrame_perListboxSelection' proc.
Loads 'PROPSin' frames and widgets into an 'execOpts' frame.
'set_PROPSin_cmd' - called by the 'exec_audio_operation' proc which is called
by the 'ExecOption' button.
Sets an 'ffmpeg' command string for showing properties
of the input media file. The command string is to be used
in the proc 'exec_audio_operation'.
--------------------------------------------------------------------------------------
'show_PROPSout_opts' - called by a button1-release binding on the listbox,
which calls the 'loadOptsFrame_perListboxSelection' proc.
Loads 'PROPSout' frames and widgets into an 'execOpts' frame.
'set_PROPSout_cmd' - called by the 'exec_audio_operation' proc which is called
by the 'ExecOption' button.
Sets an 'ffmpeg' command string for showing properties
of the output media file. The command string is to be used
in the proc 'exec_audio_operation'.
--------------------------------------------------------------------------------------
'show_CONVERT_opts' - called by a button1-release binding on the listbox,
which calls the 'loadOptsFrame_perListboxSelection' proc.
Loads 'convert' frames and widgets into an 'execOpts' frame.
'set_CONVERT_cmd' - called by the 'exec_audio_operation' proc which is called
by the 'ExecOption' button.
Sets an 'ffmpeg' command string for CONVERTING the input
media file to a new audio format.
The command string is to be used in the proc
'exec_audio_operation'.
--------------------------------------------------------------------------------------
'play_audio_file' - called by the 'PlayInAudio' and 'PlayOutAudio'buttons.
Launches an audio 'player' program to play the input
or output audio file.
'set_width_of_labels' - called in the 'Additional GUI Initialization'
section at the bottom of this script.
Sets a nice common width of the labels on the
left of each of the GUI sections:
- audio
- other
according to the font being used for labels.
'pack_more_outparms_frame' - called by button1-release binding on the
'MoreAudioOutParms' checkbutton.
Shows the 'audio' and 'other' frames of the
GUI if the 'MoreAudioOutParms' checkbutton
is ON. OR, 'forgets' the frame containing
the 'audio' and 'other' frames.
'popup_msgVarWithScroll' - called by 'Help' button to show HELPtext var.
I used the following statement to allow the GUI to be expanded in the x-direction, but NOT the y-direction. wm resizable . 1 0 whether or not the 'audio-and-other' parameter frames are showing. That is, whether or not the 'MoreMovieOutParms' checkbutton is ON or OFF. You can look for the string 'resizable' in the code to see how the 'pop-down' and 'pop-back-up' was done by toggling the y-resizing (showing and hiding of the 'MoreParms') on and off. Comments in the Code It is my hope that the copious comments in the code will help Tcl-Tk coding 'newbies' get started in making GUI's like this. Without the comments, potential young Tcler's might be tempted to return to their iPhones and iPads and iPods --- to watch 'Tsunamis Gone Wild' videos. CODE for the 'ffmpeg' Front End GUI Here is a link to the code for the Tk script: 'audioClipChgVolumeConvertEtc_ffmpeg_FrontEnd.tk' Shell script #1 (called by the Tk script) : There is one 'external' shell script that is used to implement the 'PROPERTIES-IN' and the 'PROPERTIES-OUT' options. It is needed because the 'ffmpeg' command has not been written to query the 'properties' of a media file in a 'smooth' way. This is explained more clearly in comments in the script code, below. You can put this script in the same directory with the Tk script. The Tk script includes some code (involving the 'argv0' variable) to determine the location of the shell scripts by extracting the name of the directory in which the Tk script lies. Here is a link to the code for shell script #1: 'audioFilePROPERTIES_ffmpeg-i.sh' Shell script #2 (called by the Tk script) : There is another 'external' shell script that is used as a 'wrapper' to the 'ffplay' command --- to allow this command-line program to display its execution messages --- in particular, the current elapsed-time of the audio file being played. The elapsed-time display is useful in finding the start and duration time parameters for use in the 'CLIP' option. This script starts up the 'ffplay' command (which comes with 'ffmpeg') via the 'xterm' command, so that the execution messages being sent to 'standard out' are displayed in the 'xterm' window. You can put this script in the same directory with the Tk script. The Tk script includes some code (involving the 'argv0' variable) to determine the location of the shell scripts by extracting the name of the directory in which the Tk script lies. Here is a link to the code for shell script #2: Shell script #3 (called by the Tk script) : There is another 'external' shell script that is used as a 'wrapper' to the 'mplayer' command --- to allow this command-line program to display its execution messages --- in particular, the current elapsed-time of the audio file being played. The elapsed-time display is useful in finding the start and duration time parameters for use in the 'CLIP' option. This script starts up the 'mplayer' command via the 'xterm' command, so that the execution messages being sent to 'standard out' are displayed in the 'xterm' window. You can put this script in the same directory with the Tk script. The Tk script includes some code (involving the 'argv0' variable) to determine the location of the shell scripts by extracting the name of the directory in which the Tk script lies. Here is a link to the code for shell script #3: INSTALLING THE SCRIPTS: The Tk script and the 3 shell scripts could be put in a sub-directory of the user's home directory, such as $HOME/apps/tkEditAudioFiles_ffmpeg. Then the user can use their desktop system (such as Gnome or KDE) to set up the Tk script as an icon on the desktop. Then, whenever the user wants to edit an audio file, the user can click on the icon to startup this 'front end' to the 'ffmpeg' command. SOME AUDIO PLAYERS: The following screenshot shows the execution messages that the 'mplayer' program displays in the 'xterm' window --- when the user enters 'xmplayer' in the 'Player' entry field of the GUI. |
See the elapsed-time and total-time display just below the 'Starting playback ...' line. The elapsed-time display is useful in determining start and duration times for a CLIP operation. The 'ffplay' program displays similar messages in the 'xterm' window --- when the user enters 'xffplay' in the 'Player' entry field of the GUI. However, the 'ffplay' command also starts up a 'playback' window that shows the waveform of the audio channel(s) of the audio file being played. If you want a fancier player program, the following image shows that the 'totem' player program includes an 'audio visualizer' in its GUI. |
See the elapsed time display on the lower left of the 'totem' GUI. Unfortunately, the 'totem' player program has failed to play some MOVIE files created by 'ffmpeg' and it MAY also fail on some AUDIO files created by 'ffmpeg'. If that happens, try one of the other players here. The makers of the 'mplayer' program have provided a GUI wrapper for the Mplayer program --- called 'gmplayer'. They provided 'gmplayer' back in 2009. It is not certain that they will continue to do so in the future In any case, below is a screenshot of the simple GUI of 'gmplayer'.
The display of the elapsed time in the lower left corner of this GUI can be used to help determine start and duration time parameters for a CLIP operation on an audio input file. Another popular media player program is 'VLC'. Below is a screenshot of the 'VLC' GUI.
Like with 'gmplayer', the display of the elapsed time in the lower part of this GUI (on the lower right) can be used to help determine start and duration time parameters for a CLIP operation on an audio input file. Unfortunately, like with the 'totem' player, the 'vlc' program has failed to play some MOVIE files created by 'ffmpeg' and it MAY also fail on some AUDIO files created by 'ffmpeg'. If that happens, try one of the other players here. SOME POSSIBLE ENHANCEMENTS As you can see in the listbox in the images above, I have implemented the following 'exec-options':
But there are several more options that I may implement in the future:
Also, I may return someday to the 'tkMovieEdit' utility at
Movie Clip/Crop/Convert/Etc Movie-Editor to incorporate the two shell scripts (above) that provide the 'xterm-ffplay' and 'xterm-mplayer' options to play the movie files in that utility --- instead of using the 'raw' 'ffplay' and 'mplayer' commands. There are many Tk scripts on my 'to do' list in the categories of
I need to start on these projects. But, someday, I may create another 'ffmpeg'-based utility -- 'tkKaraoke' --- to enable the user to record audio from both the speakers and the microphone of a computer system simultaneously --- in an easy-to-use way that makes it easy to do even if one does such a recording only once in a blue moon. Thus the user could sing (or talk) along to an audio file being played through the speakers, and both the voice input to a microphone attached to the computer and the audio from speaker channels will be recorded in an audio file. And this 'tkKaraoke' utility could be used in other ways. For example, play a movie (a YouTube movie, for example) and talk along with the movie. The movie soundtrack and the voice input to a microphone could be recorded in an audio file. IN CONCLUSION As I have said on several other code-donation pages on this wiki ... There's a lot to like about a utility that is 'free freedom' --- that is, no-cost and open-source so that you can modify/enhance/fix it without having to wait for someone else to do it for you (which may be never). A BIG THANK YOU to Ousterhout for starting Tcl-Tk, and a BIG THANK YOU to the Tcl-Tk developers and maintainers who have kept the simply MAH-velous 'wish' interpreter going.
2014jun30 UPDATE See a '2014jun30 UPDATE' note near the bottom of the 'tkMovieCapture_ComputerMonitorAndAudio_ ffmpegFrontEnd' page. That note contains some info on using 'avconv' instead of 'ffmpeg'. |
Bottom of this page for
To return to a previously visited web page location, click on the
Back button of your web browser a sufficient number of times.
OR, use the History-list option of your web browser.
< Go to Top of Page, above. >Page history: The code was created in 2014 --- and posted 2014 Jun 22 at http://wiki.tcl.tk/40321.
This FE web page was created 2015 Jan 17 ---
as a backup and alternative to the wiki.tcl.tk page.
NOTE: |