Using xargs and sed to reprocess avi files - bash

I have a bunch of AVI's that I'd like to reprocess to h264 MKVs. The command I'm trying to use is:
find . -name "*.avi" | xargs -I '{}' ffmpeg -i {} -vcodec libx264 `echo {} | sed 's/avi/mkv/'`
However, it does not work as expected. Adding -t to xargs shows that the command being run is (given a directory with file1.avi)
ffmpeg ./file1.avi -vcodec libx264 ./file1.avi
I'm not sure why the sed command isn't getting processed correctly. ffmpeg fails because it does not overwrite by default.
The AVIs are all in sub-directories so I don't really want to do a for loop for each subfolder and find / xargs would be a much better solution. Also, I don't really want to rename everything after this is done.

The reason why this is not working is because everything between ` ` is executed before the xargs execution. But if you quote it in ' ', then it is going to be passed correctly, but xargs is not going to parse or expand it.
But you can pipe to the bash function!
convertFile() {
while read -r filename; do
newFilename="${filename:0:-3}mkv"
ffmpeg "$filename" -vcodec libx264 "$newFilename"
done
}
find . -name "*.avi" | convertFile
Please note that instead of sed I am using bash string manipulations. The reason for that is that your file can sometimes contain avi word inside, like gravity. That is why more fail-proof way was used.

find *.avi | while read f; do
ffmpeg "$f" -vcodec libx264 "${f%.avi}.mkv"
done
and i split the lines to not scare people, but in real life it'd be
find *.avi|while read f;do ffmpeg "$f" -vcodec libx264 "${f%.avi}.mkv"; done
because this is one-liner territory.

You can use the following command instead :
find *.avi | awk '{print "\""$0"\"", "-vodec libx264","\""$0"\""}' | sed 's/\(.*\)avi\"$/\1mkv\"/g' | xargs ffmpeg -i
Of course this has a lot of escape characters. It isn't a script, it's a command-line...

Related

Use parameter expansions in a command run from "find | xargs" to prevent output overwriting

I have this bash script that is looking for mp4 files in subfolders with certain names and saves frames of those videos as jpeg.
#!/bin/bash
find ../folder -type f -iname '*C00*.mp4' | xargs -I %% ffmpeg -i %% -vf fps=1 -q:v 3 "../frames/_${i%.*}_frame%d.jpg"
The problem is that everytime the script finishes one video the .jepg output files of the next videos are overwriting the existing ones.
How can I prevent that?
Here's a quick stab which creates a directory with the same name as the input file with any .mp4 extension trimmed off.
#!/bin/bash
find ../folder -type f -iname '*C00*.mp4' -print0 |
xargs -r0 sh -c 'for f; do
d="../frames/${f%.[Mm][Pp]4}"
mkdir "$d" || { echo "$d already exists" >&2; exit 123; }
ffmpeg -i "$f" -vf fps=1 -q:v 3 "$d/frame%d.jpg"
done' _

Bash: loop ffmpeg command through sets of subfolders and direct it to files in the folders for processing

I am playing around with embedding captions into mp4 video files, and want to find a way to do this across large sets of directories with the .mp4 and .srt files in them. Each pair of .mp4 and .srt files will be subfoldered together in their own directory, and the basename should be the same between the two. Example:
Video1
Video1.mp4
Video1.srt
Video2
Video2.mp4
Video2.srt
I’ve tried several things but I’m a novice at this and only write very simple bash scripts for much more straightforward processes. For this I need to figure out how to write the bash script to run an ffmpeg command in every subfolder that will grab the mp4 and srt file and output a new mp4 of the merged data. The basic ffmpeg command to do this is:
ffmpeg -i filename.mp4 -i filename.srt -c copy -c:s mov_text output.mp4
I’ve tried to add:
for dir in ./*/; do ffmpeg -i *.mp4 -i *.srt -c copy -c:s move_text “$file”.mp4
…and several variations of this, but ffmpeg always stops with a “*.mp4: No such file or directory” error. Then I tried to add "for file in..." after the "for dir in" statement but didn't have any positive results. The following is closest to what I need - it at least goes to each folder and processes the files - but it does them independently and doesn't combine the mp4 and srt source files as the ffmpeg command should. It outputs a video.mp4.mp4 and video.srt.mp4, and fails to combine them in either case.
for dir in ./**/*;
do ffmpeg -i "$dir" -i "$dir" -c copy -c:s mov_text "$dir".mp4
I tried "$dir".mp4 and "$dir".srt but that just results in an error. I tried to pull just directory names:
for dir in ./**/*;
do ffmpeg -i "$(basename $dir)" -i "$(basename $dir)" -c copy -c:s mov_text "$dir".mp4
and my attempts using "$(basename $dir).extension" have resulted in errors - it looks for video.mp4.mp4 or video.srt.mp4. Any tips as to what to add to get this process to work or another approach entirely would be greatly appreciated! I figure it's a simple bash thing I'm just ignorant of, but certainly need to learn how to do! Thanks!
Run this within the dir containing Video1/, Video2/...
#!/bin/bash -e
shopt -s globstar
for v in ./**/*.mp4; do
s=${v%.*}.srt
if [ -f "$s" ]; then
ffmpeg -i "$v" -i "$s" -c copy -c:s mov_text "${v##*/}"
fi
done
./**/*.mp4 expands to ./Video1/Video1.mp4 ./Video1/Video2.mp4 ...,
${v%.*} removes the extension (./Video1/Video1.mp4 > ./Video1/Video1),
[ -f "$s" ] checks if $s (i.e. ./Video1/Video1.srt) exists,
${v##*/} extracts the basename of $v (./Video1/Video1.mp4 > Video1.mp4).
So the final structure of . will be like:
Video1.mp4 # subbed
Video1
Video1.mp4
Video1.srt
Video2.mp4 # subbed
Video2
Video2.mp4
Video2.srt
As a tweak to the excellent answer by ogizismail, the below is an approach that works with versions of bash too old to support globstar:
while IFS= read -r -d '' v; do
s=${v%.mp4}.srt
[[ -e $s ]] && ffmpeg -i "$v" -i "$s" -c copy -c:s mov_text "${v##*/}"
done < <(find . -mindepth 2 -name '*.mp4' -printf '%P\0')
The general technique is discussed in Using Find. Using -mindepth 2 stops it from finding your already-subbed output files.

Strange behavior about ”ls | while read" with ffmpeg in shell script [duplicate]

This question already has answers here:
Execute "ffmpeg" command in a loop [duplicate]
(3 answers)
Closed 4 years ago.
I created a shell script to convert all wave files to mp3 files. I use Ubuntu 18.04, FFmpeg was installed using apt, and I run my script in bash.
My script:
#!/bin/bash
ls *.wav | while read file
do
echo $file
ffmpeg -i "$file" -codec:a libmp3lame -b:a 192k "${file%.*}.mp3"
done
The target files are followings: (include space characters. The real filenames are longer, but I simplified the filenames. Also in this case, the problem occurs)
% ls *.wav
'01 A.wav' '02 A.wav' '03 A.wav'
The problem is that sometimes $file in the loop is blank or a part of filename strangely ('echo $file' shows that), and ffmpeg says '[broken filename]: No such file or directory'. I confirmed the following things.
When I comment out the ffmpeg line, 'echo' shows what I expected. ($file not broken)
When I replace ffmpeg to a similar command like 'lame', it works. ($file not broken)
When I replace 'ls *.wav | while read file' to 'for file in *.wav', it works. ($file not broken)
So, only when I use combination of 'ls' and 'ffmpeg', $file is broken. What's going on? Or do I misunderstand something?
You should not use ls to pipe a list of files, because this approach will not work well for corner cases like filenames with blanks.
The most readable way is to use globbing:
for file in *.wav
do
echo "$file"
ffmpeg -i "$file" -codec:a libmp3lame -b:a 192k "${file%.*}.mp3"
done
find also does a great job here, but you will end up with a little less maintainable code:
find -name *.wav -exec ffmpeg -i {} -codec:a libmp3lame -b:a 192k {}.mp3 \;
The find example will give you filenames ending in .wav.mp3 instead of .mp3, if you want to avoid that, you have to call a shell from find, allowing you to either use basename or do something like ${file.*} (you would have to assign find's {} to a variable like file in that shell first:
find -name "*.wav" -exec sh -c "file=\"{}\" ; ffmpeg -i \"\$file\" -codec:a libmp3lame -b:a 192k \${file%.*}.mp3" \;

Bash script that lists files in a directory doesn't work

I made a bash script because I need to convert a lot of files in a directory from .MOV to .mp4 format.
I created this script for the purpose:
#!/bin/bash
touch .lista
ls -1 "$1" | grep -i .MOV > .lista
list= `pwd`/.lista
cd "$1"
while read -r line;
do filename=${line%????}
ffmpeg -i "$line" -vcodec copy -acodec copy "$filename.mp4"; done < $list
rm .lista
This script is supposed to convert me each .MOV file into the directory indicated by $1, but it doesn't work, it converts me only one file, then it terminates. I can't understand why. What's wrong with that?
It's better to simply loop using globs:
for file in "$1"/*.MOV; do
ffmpeg -i "$file" ... "${file%.*}.mp4"
done
Why you shouldn't parse the output of ls.
Do them all fast and succinctly in parallel with GNU Parallel like this:
parallel --dry-run ffmpeg -i {} -vcodec copy -acodec copy {.}.mp4 ::: movies/*MOV
Sample Output
ffmpeg -i movies/a.MOV -vcodec copy -acodec copy movies/a.mp4
ffmpeg -i movies/b.MOV -vcodec copy -acodec copy movies/b.mp4
If that looks good, do it again but without --dry-run.
Note how easily GNU Parallel takes care of all the loops, all the quoting and changing the extension for you.
Your code is working for me. I cannot see any error. But I can suggest you a better approach. Don't use ls to get the filenames, it is not a good idea. Also, you can avoid changing dir.
#!/bin/bash
for line in $(find "$1" -maxdepth 1 -type f -iname "*.mov")
do
ffmpeg -i "$line" -vcodec copy -acodec copy "${line%????}.mp4"
done
You don't need to start by touching the file. In any case, you don't need a file at all, you can use a for loop to iterate over the files returned by find directly. With find, I'm already selecting all the files in the specified folder that have the expected extension.
Here I add a one-liner that should avoid problems with spaces:
find "$1" -maxdepth 1 -type f -iname "*.mov" -print0 | xargs -0 -n 1 -I{} bash -c "F={}; ffmpeg -i \"\$F\" -vcodec copy -acodec copy \"\${F%.*}\".mp4"

Bash Script to convert all flv file in a directory to mp3

This is my code so far.
#!/bin/bash
#James Kenaley
#Flv to Mp3 directory converter
find /home/downloads -iname "*.flv" | \
while read I;
do
`ffmpeg -i ${I} -acodec copy ${I/%.flv/.mp3}`
echo "$I has been converted"
done
but its picking up white spaces in the names of the flv files and throws a error saying its not in the directory. how do make it use the whole file name and not the just the first word before the space?
ffmpeg runs in forked threads, so simple batching can give weird behaviours. If you are running ffmpeg in the suggested batch loop, you should control your command and command-error output, so that it doesn't interfere.
If you run this and are getting every other item converted properly, but errors on the rest, try using this ffmpeg call in the loop:
ffmpeg -y -i "${I}" -acodec mp3 -ar 22050 -f wav "${I/%.3gp/.mp3}" > /dev/null & 2> /dev/null
Notice the > dev/null & 2> /dev/null on the end. This pipes the command output, and command error output into oblivion. Then the script works.
One should note too that the program output will look strangely disorganized, with multiple files compressing at the same time. The results will be correct.
[EDIT: NOTE THE -y THAT I HAVE, THIS MAKES FFMPEG OVERWRITE EXISTING MP3 FILES]
Try this:
`ffmpeg -i "${I}" -acodec copy "${I/%.flv/.mp3}"`
Use quotes. And don't use backquotes.
ffmpeg -i "${I}" -acodec copy "${I%.flv}".mp3
Either call a short script, to do conversion and renaming in one pass:
adhoc.sh:
$file="$1"
ffmpeg -i "$file" -acodec copy "${file/%.flv/.mp3}"
call it:
find /home/downloads -iname "*.flv" -exec ./adhoc.sh {} ";" -ls
or convert:
find /home/downloads -iname "*.flv" -exec ffmpeg -i {} -acodec copy {}.mp3 ";" -ls
and rename later:
rename 's/.flv.mp3/.mp3/' /home/downloads/*.flv.mp3
Rename is part of a perl package which might need installation.

Resources