FFMPEG Batch convert, then create folder - bash

Using this code is it possible for it run this code then create a folder with file name if folder already exits move same name to same folder?
for i in *.mkv;
do name=`echo "${i%.*}.mkv"`;
ass="ass='$name'.ass";
echo "$ass"; ffmpeg -i "$i" -vf "$ass" anime2/"${i%.mkv}.mp4";
done

You can just use mkdir -p and avoid having to use if/then:
for i in *.mkv; do mkdir -p "${i%.*}" && ffmpeg -i "$i" -vf ass="${i%.*}.ass" "${i%.*}/${i%.*}.mp4"; done

Related

GUID appended for FFMPEG output file name

Is it possible to append a GUID to the output file?
I am running:
ffmpeg -i .\Tst.mp4 -filter:v "select='gt(scene,0.5)',showinfo" -vsync 0 -s 120x68 keyframe%05d.jpg
Which produces a series of files. I need to append a GUID to every file name.
I don't believe it is possible with ffmpeg. However, you can use the MD5 muxer on the existing JPG files to provide 128-bit psuedo-UUID:
ffmpeg -v error -i "$f" -f hash -f md5 -
Result:
MD5=aa8f01566a88feb762337de3cc81f36
Bash example:
for f in *.jpg; do hash="$(ffmpeg -v error -i "$f" -f hash -f md5 -)"; mv "$f" "${f%.*}_${hash:5}.jpg"; done
Result:
keyframe_00001_aa8f01566a88feb762337de3cc81f36.jpg
keyframe_00002_49cbb3ea81acfd57edda9955b3f8ed6.jpg
keyframe_00003_31711067199dafd28a6504ec4a22e05.jpg
Not a true UUID but fine if you just need a unique identifier.

bash variable changes in loop with ffmpeg

I wrote a skript to quickly create short preview clips from vides I recorded on timestamps that I found worth checking out later for cutting.
My file with the timestamps is written like this
FILE_NAME1#MM:SS MM:SS
FILE_NAME2#MM:SS MM:SS MM:SS MM:SS
example:
MAH01728#02:47 03:34 03:44 05:00 06:08 06:55
The script looks like this:
#!/bin/bash
while read f
do
file=$(echo $f | cut -d"#" -f1)
filename=${file}".MP4"
timestamps=$(echo $f | cut -d"#" -f2)
for time in $timestamps
do
ffmpeg -ss 00:${time}.0 -i "orig/${filename}" -c copy -t 10 "preview/${file}_${time}.MP4"
done
done < $1
The script gets half of the previews that I want and on the other the filename is messed up and ffmpeg complains that the file is not found:
orig/714.MP4: No such file or directory
orig/00:58 01:25.MP4: No such file or directory
So I modified the script for trouble shooting and just put an echo in front of the ffmpeg command - now all file names are correct. What am I missing?
ffmpeg -ss 00:01:47.0 -i orig/MAH01714.MP4 -c copy -t 10 preview/MAH01714_01:47.MP4
ffmpeg -ss 00:02:00.0 -i orig/MAH01713.MP4 -c copy -t 10 preview/MAH01713_02:00.MP4
ffmpeg -ss 00:00:58.0 -i orig/MAH01712.MP4 -c copy -t 10 preview/MAH01712_00:58.MP4
ffmpeg -ss 00:01:25.0 -i orig/MAH01712.MP4 -c copy -t 10 preview/MAH01712_01:25.MP4
ffmpeg reads from standard input, consuming data from $1 that was intended for the read command at the top of the loop. Redirect its standard input from /dev/null:
while IFS="#" read file timestamps; do
filename="$file.MP4"
for time in $timestamps; do
ffmpeg -ss 00:${time}.0 -i "orig/${filename}" \
-c copy -t 10 "preview/${file}_${time}.MP4" < /dev/null
done
done < "$1"
echo does not read from standard input, which is why your modification made it appear to be working correctly.

ffmpeg with shell: oncat listed movies

I'm new to shell script, trying to concat listed movies in a folder like:
filename="list.txt"
cat ${filename} | while read line;
do
ffmpeg -y -i sum.mov -i $line -filter_complex concat tmp.mov
cp tmp.mov sum.mov
done
However, this loop runs only one time.
What is the best practice?
Thanks
SOLVED:
#list.txt
file 'movie1.mov'
file 'movie2.mov'
file 'movie3.mov'
Don't need to write a shell script. Just
ffmpeg -f concat -i list.txt -c copy output.mov

bash script behaves differently when run from an SD card or from HD

I have this command in a bash script:
mv audio.mp3 $datadir/$newname
where $datadir is a directory created at the beginning of the script:
datadir=$(date +%Y-%m-%d_%H-%M)
mkdir -p $datadir
while the rest if the script is
wget -q "$url" -O audio.mp3
poddate=$(stat -c "%y" "audio.mp3"|awk '{print $1"_"$2}'|sed 's/\..*$//')
extmp3=.mp3
seprdr=_
newname=$podname$seprdr$poddate$extmp3
echo -e "${GREEN}\t$newname"
mv audio.mp3 $datadir/$newname
the script runs fine when it is located on the hard drive.
Now, if it is run from an SD card, I get the following error:
cannot move audio.mp3 to a subdirectory of itself
what is wrong?
thanks

#/bin/sh in one line

I'm working on some Haskell project using FFmpeg. I need to batch create from a media folder with MP4 files and create screenshots from all of them. I got the code and am using it on a terminal in Unix. It works, but how do I make it in one line to be executed in system "xxxx" in Haskell?
If not using several system"xx"...
#/bin/sh
for i in $(ls *.mp4)
do
ffmpeg -i $i -vframes 7 -y -ss 10 -s 150x150 -an -sameq -f image2 -r 1/5 $i%1d.jpg
done
I tried:
import System.Cmd
function = do{system "#/bin/sh";
system "for i in $(ls *.mp4)";
system "do";
system "ffmpeg -i $i -vframes 7 -y -ss 10 -s 150x150 -an -sameq -f image2 -r 1/5 $i%1d.jpg";
system "done";}
but it gives a error:
-vframes: No such file or directory
/bin/sh: Syntax error: "done" unexpected
The problem is that you're trying to execute each line of your script as a separate, independent invocation of the shell. You just need to do it all with one system call, and separate each line of the script with \n:
system "for i in $(ls *.mp4)\ndo\n..."
but you can write the shell command on one logical line, instead:
system "for i in $(ls *.mp4); do ...; done"
The first line (which should be #!/bin/sh, by the way) is not necessary when using system.
I'm not sure why you want to use Haskell for this purpose, though, if you're just going to execute a single shell script. You should write the loop over the directory contents in Haskell, and only call out to the system to do an individual conversion. At the very least, you should probably put this script into its own file and invoke it with system "sh convert.sh" or similar.
(If you want a more convenient syntax for multi-line strings like these scripts in Haskell, try the interpolatedstring-perl6 or string-qq packages.)
First, It's #!/bin/sh. Notice the exclamation mark.
Second, you're trying to execute a series of commands one after another, so no state is kept between them. Try to execute it as a single command:
function = system "for i in $(ls *.mp4); do ffmpeg -i $i -vframes 7 -y -ss 10 -s 150x150 -an -sameq -f image2 -r 1/5 $i%1d.jpg; done"
Another option is to save your whole script, with the #! corrected, as a .sh file, make it executable and:
function = system "./myscript.sh"
Bash 4.X Solution
system "/bin/bash -c 'shopt -s globstar; for i in **.mp4; do ffmpeg -i \"$i\" -vframes 7 -y -ss 10 -s 150x150 -an -sameq -f image2 -r 1/5 \"$i\"%1d.jpg; done'"
You don't need #!/bin/bash with system (don't forget the bang !)
Quote your variables otherwise files with spaces in their names wont work
Don't use ls like that, it will break when it comes across a file with spaces in its name
Posix Solution
system "find /some/path -type f -name \"*.mp4\" -exec sh -c 'for f; do ffmpeg -i \"$f\" -vframes 7 -y -ss 10 -s 150x150 -an -sameq -f image2 -r 1/5 \"$f%1d.jpg\"; done' _ {} +"
You should not echo the shell script like this but create a shell command like this:
system "for i in $(ls *.mp4); do ffmpeg -i $i -vframes 7 -y -ss 10 -s 150x150 -an -sameq -f image2 -r 1/5 $i%1d.jpg; done"

Resources