ffmpeg
is a command-line tool for transcoding (converting) media like video and audio files. It is extraordinarily powerful, but also not terribly easy or convenient to use. That's where the script comes in: Your goal is to set up a few defaults for yourself for when you need to use ffmpeg
so that you don't have to remember or type out the whole command line.
While not strictly necessary, we will also be adding argument parsing, loops for dealing with multiple files, and error handling.
Your script must accomplish the following things:
- Process command line arguments.
- One for converting video to a predefined format
- One for just converting audio
- Provide a helpful error message and exit when something unexpected happens.
- Process multiple files.
- Show progress for processing multiple files.
- Make use of the following Bash constructs:
case
statementsif
conditionsfor
loopswhile
loops- functions
trap
s- local and global variables
- variable default values
- variable substitution
- arrays
shift
- pipes
logger
- math expressions
#!/bin/bash
trap exit on signal
array files = []
variable counter = 1
variable conversion_type = none
variable destination_directory = here
variable video_options = things
variable audio_options = stuff
while loop arguments
case argument
is it a directory?
set destination directory
is it a media type?
set media type
is it a file?
is it a valid media file?
add it to the file array
else say that's nonsense and exit
else
say that the argument was bad and you should feel bad
quit
next argument
pipe all output of while loop to logger and standard error
print info on what we are about to do to logger and standard error
function convert file
local variable full path to input file (taken from function argument)
local variable input filename (derived from input file path)
local variable destination filename (derived from source file name and destination file type)
print file name, file counter, and how many files there are in total
ffmpeg input options output
quit script on errors
for loop on file array
call convert function with file path as argument
increment counter
pipe all output of for loop to logger and standard error
say we're done here
- http://www.tldp.org/LDP/Bash-Beginners-Guide/html/
- https://wiki.bash-hackers.org/start
- https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash
- https://intoli.com/blog/exit-on-errors-in-bash-scripts/
- https://linux.die.net/man/1/logger
I will not expect you to know ffmpeg for this challenge. That is a whole different skill set. I will fill out the ffmpeg
options for you here:
Video, which must have a .mkv
extension: -loglevel error -stats -ac 2 -b:v 0 -c:a libopus -c:v libvpx-vp9 -crf 24 -threads 8 -speed 8 -tile-columns 6 -frame-parallel 1 -strict -2
Audio, which must have a .opus
file extension: -loglevel error -stats -vn -c:a libopus -ac 2
You can download ffmpeg from here: https://evermeet.cx/ffmpeg/
You may need VLC to play back the files: http://www.videolan.org/