There's a printf
tool that simulates the C function; normally it's at /usr/bin/printf
, but a lot of shells implement built-ins for it as well. You can use %02x
to get the hex representation of a character, but you need to make sure you pass a string that includes the character in single-quotes (Edit: It turns out just a single-quote at the beginning is sufficient):
printf "%%%02x\n" "'-" # Outputs %2d
You can make a shell function for convenience:
function hex() {
printf "%%%02x\n" "'$1"
}
hex - # Outputs %2d
hex _ # Outputs %5f
hex . # Outputs %2e
BASH isn't that bad for this problem. You just need to use the very powerful, but underused date
command.
for i in {1..10}; do
hrmin=$(date -u -d@$(($i * 10 * 60)) +"%H:%M")
outfile=${hrmin/:/-}.mp4
ffmpeg -i video.mp4 -ss ${hrmin}:00 -t 00:10:00 -c copy ${outfile}
done
date
Command Explained
date
with a -d
flags allows you to set which date you want displayed (instead of the current date and time, which is the default). In this case, I am setting it to a UNIX time by prepending the @
symbol before an integer. The integer in this case is the time in ten minute increments (calculated by the BASH built-in calculator: $((...))
).
The +
symbol tells date
that you would like to specify a format for displaying the results. In our case, we care only about the hour (%H
) and the minutes (%M
).
And finally, the -u
is to display as UTC time instead of local. This is important in this case because we specified the time as UTC when we gave it the UNIX time (UNIX time is always as UTC). The numbers would most likely not start from 0 if you didn't specify -u
.
BASH Variable Substitution Explained
The date
command gave us just what we needed. But colons in a file name might be problematic/non-standard. So, we substitute the ':' for a '-'. This can be done by the sed
or cut
or tr
command, but because this is such a simple task, why spawn a new subshell when BASH can do it?
Here we use BASH's simple expression substitution. To do this, the variable must be contained within curly braces (${hrmin}
) and then use the standard forward slash notation. The first string after the first slash is the search pattern. The second string after the second slash is the substitution.
BASH variable substitution and more can be found at http://tldp.org/LDP/abs/html/parameter-substitution.html.
Best Answer
In bash:
Use:
Or more POSIX_ly:
Use:
If Value is hex: