0

I am using raspbian for a project.

Here is my situation. I'm creating three files:

  1. a wav file with a filename format of [date_and_timestamp].wav.
  2. an avi file with a filename format of [date_and_timestamp].avi.
  3. a file with the name marker_[date_and_timestamp]

The value of [date_and_timestamp] is consistent across all three files.

What I want to do is look for all files starting with marker_, get the [date_and_timestamp] portion and use it to pass as a parameter to ffmpeg to combine the two files (i.e. ending up with a command that looks like

ffmpeg -i /home/motion/[date_and_timestamp].avi -i /home/motion/[date_and_timestamp].wav /home/motion/[date_and_timestamp].mp4

Given how powerful sh is, I know there will be a way, but I am far to n00bish to know what it is.

The square brackets are there for clarity. They do not appear in the filenames.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Possible duplicate of [Looping over pairs of values in bash](http://stackoverflow.com/questions/28725333/looping-over-pairs-of-values-in-bash) – tripleee Apr 05 '17 at 05:19

2 Answers2

0

You seem to be looking for something like this.

for marker in marker_*; do
    ffmpeg -i "${marker#marker_}.avi" -i "${marker#marker_}.wav" "${marker#marker_}.mp4"
done

Run it in the correct directory and you won't have to hard-code the path names in the ffmpeg command.

The notation ${variable#prefix} yields the value of variable with prefix removed if present. There is a similar construct with % to remove a suffix.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • This is epic. I was attempting to do it with awk -> an array, then iterating the array. Your approach is much more elegant. Thanks! – JoAnywhere Apr 06 '17 at 03:57
0
ls -l marker_* | awk -F[ '{ system("ffmpeg -i /home/motion/["$2".avi -i /home/motion/["$2".wav /home/motion/["$2".mp4") }'

This will list all files beginning with marker_ and pass this to awk, extracting the date_and_timestamp ($2 delimited by [) and then use this to pass the date to the awk system function and form a command to be executed. Place particular attention to the quotation marks when building a command to be used with the system function.

  • [Don¨t parse `ls`](http://mywiki.wooledge.org/ParsingLs) -- it's not doing anything useful, the shell already expands the wildcard. `printf "%s\n" marker_*` would be a slightly more robust and correct way to drive the pipeline, but still doesn't correctly handle file names which contain newlines. – tripleee Apr 06 '17 at 04:26