D&C GLug - Home Page

[ Date Index ] [ Thread Index ] [ <= Previous by date / thread ] [ Next by date / thread => ]

[LUG]Re: Shell Scripting question

 

Hi Peter & Mr comrade meowski

Thanks for this, works great, having several solutions is really useful to this. I have taken peters solution and added some extra code to store the appended text in a variable then add it to the new filename, took a bit of experimentation but figured it out.

With the one liner, I will take a much closer look to see what it is doing.

Paul

On 26/12/2024 16:31, Peter Walker via list wrote:
Hi Paul,

Post Christmas and Boxing Day dinners so no guarantees on details but this is nice and easy to do in bash.

Like everything in Linux there are many ways of doing it so just build from this.

You already have your files in a directory so you  don't want to list them explicitly. If we assume we are in the directory then the linux globbing will take care of that.
Eg.
ls *.mp3

Globbing just matches filenames based on the pattern you provide.

Your for loop was the right way to go and you just need to feed it the correct list of files. Splitting the file names to make new ones is an extra command so the easy option is to create a new directory (new) to copy to and use the original names.


for file in *.mp3
do
     ffmpeg -i $file -c copy -an new/${file}
done

To make it work with new file names we need an extra step.

for file in *.mp3
do
     filename="${file%.*}"
     ffmpeg -i $file -c copy -an ${filename}-ns.mp3
done

As I said, there are lots of ways of doing this sort of think. Google xargs and bash scripting for more.

Cheers,
Pete


On 26/12/2024 11:37, zleap via list wrote:
Hi, all

I am trying to write a shell script to auto mate a task of stripping sound from some video files,  as I am fairly new to writing scripts I have so far found part of a solution

I have modified this
#!/bin/bash
echo Create files listed in an array with the touch command
files=(
  # list of files to create
  "file1.txt"
  "file2.txt"
  "file3.txt"
)

# Loop to create each file
for filelist in "${files[@]}"; do
  touch "$filelist"
done

To include the target file names in the array

The following will strip from a single file

ffmpeg -i 20OCT5.mp4 -c copy -an 20OCT5-ns.mp4

So far I have modified so it applies the source file(s) listed in the array

ffmpeg -i $filelist -c copy -an 20OCT5-ns.mp4

I am not sure what to put in to tell the script to slightly modify the filename so the output file is modified for example

if the inputfile is
video.mp4 the output file with sound stripped is something like video- ns.mp4

Can anyone advise further please, it is probably fairly simple for someone with more shell scripting experience.

Thanks

Paul




--
Paul Sutton, Cert Cont Sci (Open)
https://personaljournal.ca/paulsutton/
Mastodon : @zleap@xxxxxxxx
--
The Mailing List for the Devon & Cornwall LUG
FAQ: https://www.dcglug.org.uk/faq/