convert metadata value to string while doing ffmpeg conversion - bash

I have a script which splits mp3 files into smaller files and incrementally numbers them. I'm now also trying to incrementally increase the -metadata title number value and -metadata track number value but ffmpeg sees everything as a string. The line I'm having issue with is.
ffmpeg -i "$f" -f segment -segment_time 1200 -ar 22050 -ac 1 -metadata title="$%03d-$fn.mp3" -metadata track="$%02d" "$splitdirname/%03d-$fn.mp3" #split every 20mins
If you look at the metadata title created it says $%03d-title_of_file.mp3 I'm trying to get the metatdata title to increment like 001-title_of_file.mp3, 002-title_of_file.mp3, 003-title_of_file.mp3,...
The full script i'm using is below:
#!/bin/bash
#run using bash mp3spl.sh
currentdir="$(pwd)" #get current directory
for f in *.mp3 # only look for mp3 files
do
#fn=`echo "$1" | cut -d'.' -f1` #get just the filename no extension
fn=$(basename "$f" | cut -d'.' -f1) #get just the filename no extension
echo "($fn)"
splitdirname="$currentdir/split-$fn" #sub directory with correct names
#echo "splitdirname $splitdirname"
mkdir -p "$splitdirname" #make split directory
#echo "Processing $f"
ffmpeg -i "$1" 2> tmp.txt
ffmpeg -i "$f" -f segment -segment_time 1200 -ar 22050 -ac 1 -metadata title="$%03d-$fn.mp3" -metadata track="$%02d" "$splitdirname/%03d-$fn.mp3" #split every 20mins
#rm tmp.txt
done
Using -metadata title="$%03d-$fn.mp3" -metadata track="$%02d" does not create incremental numbers with leading zeros when used with ffmpeg.

The %03d syntax is only valid for input or output file patterns, for those (de)muxers that implement this kind of parsing (e.g., image2, segment). You cannot use it for setting metadata attributes. In other words, you can't have ffmpeg populate the field with the current segment filename.
Also, the $ in $%03d does not make sense—you're not referring to a shell variable here.
If you want to set the metadata according to the filename of the file that is generated, you have to do this in a second pass. Loop over each generated file, parse its filename, and use that to set the metadata value. Copy the existing audio/video streams with -c copy and -map 0 (the latter is necessary if you have more than one audio/video stream).

Related

Extract Creation date and time of multiple files at once [duplicate]

How do you convert an entire directory/folder with ffmpeg via command line or with a batch script?
For Linux and macOS this can be done in one line, using parameter expansion to change the filename extension of the output file:
for i in *.avi; do ffmpeg -i "$i" "${i%.*}.mp4"; done
Previous answer will only create 1 output file called out.mov. To make a separate output file for each old movie, try this.
for i in *.avi;
do name=`echo "$i" | cut -d'.' -f1`
echo "$name"
ffmpeg -i "$i" "${name}.mov"
done
And on Windows:
FOR /F "tokens=*" %G IN ('dir /b *.flac') DO ffmpeg -i "%G" -acodec mp3 "%~nG.mp3"
For Windows:
Here I'm Converting all the (.mp4) files to (.mp3) files.
Just open cmd, goto the desired folder and type the command.
Shortcut: (optional)
1. Goto the folder where your (.mp4) files are present
2. Press Shift and Left click and Choose "Open PowerShell Window Here"
or "Open Command Prompt Window Here"
3. Type "cmd" [NOTE: Skip this step if it directly opens cmd instead of PowerShell]
4. Run the command
for %i in (*.mp4) do ffmpeg -i "%i" "%~ni.mp3"
If you want to put this into a batch file on Windows 10, you need to use %%i.
A one-line bash script would be easy to do - replace *.avi with your filetype:
for i in *.avi; do ffmpeg -i "$i" -qscale 0 "$(basename "$i" .avi)".mov ; done
To convert with subdirectories use e.g.
find . -exec ffmpeg -i {} {}.mp3 \;
#Linux
To convert a bunch, my one liner is this, as example
(.avi to .mkv) in same directory:
for f in *.avi; do ffmpeg -i "${f}" "${f%%.*}.mkv"; done
please observe the double "%%" in the output statement. It gives you not only the first word or the input filename, but everything before the last dot.
For anyone who wants to batch convert anything with ffmpeg but would like to have a convenient Windows interface, I developed this front-end:
https://sourceforge.net/projects/ffmpeg-batch
It adds to ffmpeg a window fashion interface, progress bars and time remaining info, features I always missed when using ffmpeg.
Of course, now PowerShell has come along, specifically designed to make something exactly like this extremely easy.
And, yes, PowerShell is also available on other operating systems other than just Windows, but it comes pre-installed on Windows, so this should be useful to everyone.
First, you'll want to list all of the files within the current directory, so, we'll start off with:
ls
You can also use ls -Recurse if you want to recursively convert all files in subdirectories too.
Then, we'll filter those down to only the type of file we want to convert - e.g. "avi".
ls | Where { $_.Extension -eq ".avi" }
After that, we'll pass that information to FFmpeg through a ForEach.
For FFmpeg's input, we will use the FullName - that's the entire path to the file. And for FFmpeg's output we will use the Name - but replacing the .avi at the end with .mp3. So, it will look something like this:
$_.Name.Replace(".avi", ".mp3")
So, let's put all of that together and this is the result:
ls | Where { $_.Extension -eq ".avi" } | ForEach { ffmpeg -i $_.FullName $_.Name.Replace(".avi", ".mp3") }
That will convert all ".avi" files into ".mp3" files through FFmpeg, just replace the three things in quotes to decide what type of conversion you want, and feel free to add any other arguments to FFmpeg within the ForEach.
You could take this a step further and add Remove-Item to the end to automatically delete the old files.
If ffmpeg isn't in your path, and it's actually in the directory you're currently in, write ./ffmpeg there instead of just ffmpeg.
Hope this helps anyone.
If you have GNU parallel you could convert all .avi files below vid_dir to mp4 in parallel, using all except one of your CPU cores with
find vid_dir -type f -name '*.avi' -not -empty -print0 |
parallel -0 -j -1 ffmpeg -loglevel fatal -i {} {.}.mp4
To convert from/to different formats, change '*.avi' or .mp4 as needed. GNU parallel is listed in most Linux distributions' repositories in a package which is usually called parallel.
Using multiple cores, this is the fastest way, (using parallel):
parallel "ffmpeg -i {1} {1.}.mp4" ::: *.avi
I know this might be redundant but I use this script to batch convert files.
old_extension=$1
new_extension=$2
for i in *."$old_extension";
do ffmpeg -i "$i" "${i%.*}.$new_extension";
done
It takes 2 arguments to make it more flexible :
the extension you want to convert from
the new extension you want to convert to
I create an alias for it but you can also use it manually like this:
sh batch_convert.sh mkv mp4
This would convert all the mkv files into mp4 files.
As you can see it slightly more versatile. As long as ffmpeg can convert it you can specify any two extensions.
The following script works well for me in a Bash on Windows (so it should work just as well on Linux and Mac). It addresses some problems I have had with some other solutions:
Processes files in subfolders
Replaces the source extension with the target extension instead of just appending it
Works with files with multiple spaces and multiple dots in the name
(See this answer for details.)
Can be run when the target file exists, prompting before overwriting
ffmpeg-batch-convert.sh:
sourceExtension=$1 # e.g. "mp3"
targetExtension=$2 # e.g. "wav"
IFS=$'\n'; set -f
for sourceFile in $(find . -iname "*.$sourceExtension")
do
targetFile="${sourceFile%.*}.$targetExtension"
ffmpeg -i "$sourceFile" "$targetFile"
done
unset IFS; set +f
Example call:
$ sh ffmpeg-batch-convert.sh mp3 wav
As a bonus, if you want the source files deleted, you can modify the script like this:
sourceExtension=$1 # e.g. "mp3"
targetExtension=$2 # e.g. "wav"
deleteSourceFile=$3 # "delete" or omitted
IFS=$'\n'; set -f
for sourceFile in $(find . -iname "*.$sourceExtension")
do
targetFile="${sourceFile%.*}.$targetExtension"
ffmpeg -i "$sourceFile" "$targetFile"
if [ "$deleteSourceFile" == "delete" ]; then
if [ -f "$targetFile" ]; then
rm "$sourceFile"
fi
fi
done
unset IFS; set +f
Example call:
$ sh ffmpeg-batch-convert.sh mp3 wav delete
I use this for add subtitle for Tvshows or Movies on Windows.
Just create "subbed" folder and bat file in the video and sub directory.Put code in bat file and run.
for /R %%f in (*.mov,*.mxf,*.mkv,*.webm) do (
ffmpeg.exe -i "%%~f" -i "%%~nf.srt" -map 0:v -map 0:a -map 1:s -metadata:s:a language=eng -metadata:s:s:1 language=tur -c copy ./subbed/"%%~nf.mkv"
)
Getting a bit like code golf here, but since nearly all the answers so far are bash (barring one lonely cmd one), here's a windows cross-platform command that uses powershell (because awesome):
ls *.avi|%{ ffmpeg -i $_ <ffmpeg options here> $_.name.replace($_.extension, ".mp4")}
You can change *.avi to whatever matches your source footage.
Also if you want same convertion in subfolders.
here is the recursive code.
for /R "folder_path" %%f in (*.mov,*.mxf,*.mkv,*.webm) do (
ffmpeg.exe -i "%%~f" "%%~f.mp4"
)
for i in *.flac;
do name=`echo "${i%.*}"`;
echo $name;
ffmpeg -i "${i}" -ab 320k -map_metadata 0 -id3v2_version 3 "${name}".mp3;
done
Batch process flac files into mp3 (safe for file names with spaces) using [1] [2]
windows:
#echo off
for /r %%d in (*.wav) do (
ffmpeg -i "%%~nd%%~xd" -codec:a libmp3lame -c:v copy -qscale:a 2 "%
%~nd.2.mp3"
)
this is variable bitrate of quality 2, you can set it to 0 if you want but unless you have a really good speaker system it's worthless imo
Only this one Worked for me, pls notice that you have to create "newfiles" folder manually where the ffmpeg.exe file is located.
Convert . files to .wav audio
Code:
for %%a in ("*.*") do ffmpeg.exe -i "%%a" "newfiles\%%~na.wav"
pause
i.e if you want to convert all .mp3 files to .wav change ("*.*") to ("*.mp3").
The author of this script is :
https://forum.videohelp.com/threads/356314-How-to-batch-convert-multiplex-any-files-with-ffmpeg
hope it helped 🙏.
For giggles, here's solution in fish-shell:
for i in *.avi; ffmpeg -i "$i" (string split -r -m1 . $i)[1]".mp4"; end
Bash is terrible to me, so under Linux/Mac, I prefer Ruby script:
( find all the files in a folder and then convert it from rmvb/rm format to mp4 format )
# filename: run.rb
Dir['*'].each{ |rm_file|
next if rm_file.split('.').last == 'rb'
command = "ffmpeg -i '#{rm_file}' -c:v h264 -c:a aac '#{rm_file.split('.')[0]}.mp4'"
puts "== command: #{command}"
`#{command}`
}
and you can run it with: ruby run.rb
Alternative approach using fd command (repository):
cd directory
fd -d 1 mp3 -x ffmpeg -i {} {.}.wav
-d means depth
-x means execute
{.} path without file extension
I developed a python package for this case.
https://github.com/developer0hye/BatchedFFmpeg
You can easily install and use it.
pip install batchedffmpeg
batchedffmpeg * -i folder * output_file
This will create mp4 video from all the jpg files from current directory.
echo exec("ffmpeg -framerate 1/5 -i photo%d.jpg -r 25 -pix_fmt yuv420p output.mp4");
I'm using this one-liner in linux to convert files (usually H265) into something I can play on Kodi without issues:
for f in *.mkv; do ffmpeg -i "$f" -c:v libx264 -crf 28 -c:a aac -b:a 128k output.mkv; mv -f output.mkv "$f"; done
This converts to a temporary file and then replaces the original so the names remain the same after conversion.
I needed all the videos to use the same codec for merging purposes
so this conversion is mp4 to mp4
it's in zsh but should easily be convertible to bash
for S (*.mp4) { ffmpeg -i $S -c:v libx264 -r 30 new$S }
If you want a graphical interface to batch process with ffmpegX, try Quick Batcher. It's free and will take your last ffmpegX settings to convert files you drop into it.
Note that you can't drag-drop folders onto Quick Batcher. So select files and then put them through Quick Batcher.
Another simple solution that hasn't been suggested yet would be to use xargs:
ls *.avi | xargs -i -n1 ffmpeg -i {} "{}.mp4"
One minor pitfall is the awkward naming of output files (e.g. input.avi.mp4). A possible workaround for this might be:
ls *.avi | xargs -i -n1 bash -c "i={}; ffmpeg -i {} "\${i%.*}.mp4""
And for Windows, this does not work
FOR /F "tokens=*" %G IN ('dir /b *.flac') DO ffmpeg -i "%G" -acodec mp3 "%~nG.mp3"
even if I do double those %.
I would even suggest:
-acodec ***libmp3lame***
also:
FOR /F "tokens=*" %G IN ('dir /b *.flac') DO ffmpeg -i "%G" -acodec libmp3lame "%~nG.mp3"
This is what I use to batch convert avi to 1280x mp4
FOR /F "tokens=*" %%G IN ('dir /b *.avi') DO "D:\Downloads\ffmpeg.exe" -hide_banner -i "%%G" -threads 8 -acodec mp3 -b:a 128k -ac 2 -strict -2 -c:v libx264 -crf 23 -filter:v "scale=1280:-2,unsharp=5:5:1.0:5:5:0.0" -sws_flags lanczos -b:v 1024k -profile:v main -preset medium -tune film -async 1 -vsync 1 "%%~nG.mp4"
Works well as a cmd file, run it, the loop finds all avi files in that folder.
calls MY (change for yours) ffmpeg, passes input name, the settings are for rescaling up with sharpening. I probs don't need CRF and "-b:v 1024k"...
Output file is input file minus the extension, with mp4 as new ext.

FFMPEG Multiple task for converting video to image sequence for MacOS [duplicate]

How do you convert an entire directory/folder with ffmpeg via command line or with a batch script?
For Linux and macOS this can be done in one line, using parameter expansion to change the filename extension of the output file:
for i in *.avi; do ffmpeg -i "$i" "${i%.*}.mp4"; done
Previous answer will only create 1 output file called out.mov. To make a separate output file for each old movie, try this.
for i in *.avi;
do name=`echo "$i" | cut -d'.' -f1`
echo "$name"
ffmpeg -i "$i" "${name}.mov"
done
And on Windows:
FOR /F "tokens=*" %G IN ('dir /b *.flac') DO ffmpeg -i "%G" -acodec mp3 "%~nG.mp3"
For Windows:
Here I'm Converting all the (.mp4) files to (.mp3) files.
Just open cmd, goto the desired folder and type the command.
Shortcut: (optional)
1. Goto the folder where your (.mp4) files are present
2. Press Shift and Left click and Choose "Open PowerShell Window Here"
or "Open Command Prompt Window Here"
3. Type "cmd" [NOTE: Skip this step if it directly opens cmd instead of PowerShell]
4. Run the command
for %i in (*.mp4) do ffmpeg -i "%i" "%~ni.mp3"
If you want to put this into a batch file on Windows 10, you need to use %%i.
A one-line bash script would be easy to do - replace *.avi with your filetype:
for i in *.avi; do ffmpeg -i "$i" -qscale 0 "$(basename "$i" .avi)".mov ; done
To convert with subdirectories use e.g.
find . -exec ffmpeg -i {} {}.mp3 \;
#Linux
To convert a bunch, my one liner is this, as example
(.avi to .mkv) in same directory:
for f in *.avi; do ffmpeg -i "${f}" "${f%%.*}.mkv"; done
please observe the double "%%" in the output statement. It gives you not only the first word or the input filename, but everything before the last dot.
For anyone who wants to batch convert anything with ffmpeg but would like to have a convenient Windows interface, I developed this front-end:
https://sourceforge.net/projects/ffmpeg-batch
It adds to ffmpeg a window fashion interface, progress bars and time remaining info, features I always missed when using ffmpeg.
Of course, now PowerShell has come along, specifically designed to make something exactly like this extremely easy.
And, yes, PowerShell is also available on other operating systems other than just Windows, but it comes pre-installed on Windows, so this should be useful to everyone.
First, you'll want to list all of the files within the current directory, so, we'll start off with:
ls
You can also use ls -Recurse if you want to recursively convert all files in subdirectories too.
Then, we'll filter those down to only the type of file we want to convert - e.g. "avi".
ls | Where { $_.Extension -eq ".avi" }
After that, we'll pass that information to FFmpeg through a ForEach.
For FFmpeg's input, we will use the FullName - that's the entire path to the file. And for FFmpeg's output we will use the Name - but replacing the .avi at the end with .mp3. So, it will look something like this:
$_.Name.Replace(".avi", ".mp3")
So, let's put all of that together and this is the result:
ls | Where { $_.Extension -eq ".avi" } | ForEach { ffmpeg -i $_.FullName $_.Name.Replace(".avi", ".mp3") }
That will convert all ".avi" files into ".mp3" files through FFmpeg, just replace the three things in quotes to decide what type of conversion you want, and feel free to add any other arguments to FFmpeg within the ForEach.
You could take this a step further and add Remove-Item to the end to automatically delete the old files.
If ffmpeg isn't in your path, and it's actually in the directory you're currently in, write ./ffmpeg there instead of just ffmpeg.
Hope this helps anyone.
If you have GNU parallel you could convert all .avi files below vid_dir to mp4 in parallel, using all except one of your CPU cores with
find vid_dir -type f -name '*.avi' -not -empty -print0 |
parallel -0 -j -1 ffmpeg -loglevel fatal -i {} {.}.mp4
To convert from/to different formats, change '*.avi' or .mp4 as needed. GNU parallel is listed in most Linux distributions' repositories in a package which is usually called parallel.
Using multiple cores, this is the fastest way, (using parallel):
parallel "ffmpeg -i {1} {1.}.mp4" ::: *.avi
I know this might be redundant but I use this script to batch convert files.
old_extension=$1
new_extension=$2
for i in *."$old_extension";
do ffmpeg -i "$i" "${i%.*}.$new_extension";
done
It takes 2 arguments to make it more flexible :
the extension you want to convert from
the new extension you want to convert to
I create an alias for it but you can also use it manually like this:
sh batch_convert.sh mkv mp4
This would convert all the mkv files into mp4 files.
As you can see it slightly more versatile. As long as ffmpeg can convert it you can specify any two extensions.
The following script works well for me in a Bash on Windows (so it should work just as well on Linux and Mac). It addresses some problems I have had with some other solutions:
Processes files in subfolders
Replaces the source extension with the target extension instead of just appending it
Works with files with multiple spaces and multiple dots in the name
(See this answer for details.)
Can be run when the target file exists, prompting before overwriting
ffmpeg-batch-convert.sh:
sourceExtension=$1 # e.g. "mp3"
targetExtension=$2 # e.g. "wav"
IFS=$'\n'; set -f
for sourceFile in $(find . -iname "*.$sourceExtension")
do
targetFile="${sourceFile%.*}.$targetExtension"
ffmpeg -i "$sourceFile" "$targetFile"
done
unset IFS; set +f
Example call:
$ sh ffmpeg-batch-convert.sh mp3 wav
As a bonus, if you want the source files deleted, you can modify the script like this:
sourceExtension=$1 # e.g. "mp3"
targetExtension=$2 # e.g. "wav"
deleteSourceFile=$3 # "delete" or omitted
IFS=$'\n'; set -f
for sourceFile in $(find . -iname "*.$sourceExtension")
do
targetFile="${sourceFile%.*}.$targetExtension"
ffmpeg -i "$sourceFile" "$targetFile"
if [ "$deleteSourceFile" == "delete" ]; then
if [ -f "$targetFile" ]; then
rm "$sourceFile"
fi
fi
done
unset IFS; set +f
Example call:
$ sh ffmpeg-batch-convert.sh mp3 wav delete
I use this for add subtitle for Tvshows or Movies on Windows.
Just create "subbed" folder and bat file in the video and sub directory.Put code in bat file and run.
for /R %%f in (*.mov,*.mxf,*.mkv,*.webm) do (
ffmpeg.exe -i "%%~f" -i "%%~nf.srt" -map 0:v -map 0:a -map 1:s -metadata:s:a language=eng -metadata:s:s:1 language=tur -c copy ./subbed/"%%~nf.mkv"
)
Getting a bit like code golf here, but since nearly all the answers so far are bash (barring one lonely cmd one), here's a windows cross-platform command that uses powershell (because awesome):
ls *.avi|%{ ffmpeg -i $_ <ffmpeg options here> $_.name.replace($_.extension, ".mp4")}
You can change *.avi to whatever matches your source footage.
Also if you want same convertion in subfolders.
here is the recursive code.
for /R "folder_path" %%f in (*.mov,*.mxf,*.mkv,*.webm) do (
ffmpeg.exe -i "%%~f" "%%~f.mp4"
)
for i in *.flac;
do name=`echo "${i%.*}"`;
echo $name;
ffmpeg -i "${i}" -ab 320k -map_metadata 0 -id3v2_version 3 "${name}".mp3;
done
Batch process flac files into mp3 (safe for file names with spaces) using [1] [2]
windows:
#echo off
for /r %%d in (*.wav) do (
ffmpeg -i "%%~nd%%~xd" -codec:a libmp3lame -c:v copy -qscale:a 2 "%
%~nd.2.mp3"
)
this is variable bitrate of quality 2, you can set it to 0 if you want but unless you have a really good speaker system it's worthless imo
Only this one Worked for me, pls notice that you have to create "newfiles" folder manually where the ffmpeg.exe file is located.
Convert . files to .wav audio
Code:
for %%a in ("*.*") do ffmpeg.exe -i "%%a" "newfiles\%%~na.wav"
pause
i.e if you want to convert all .mp3 files to .wav change ("*.*") to ("*.mp3").
The author of this script is :
https://forum.videohelp.com/threads/356314-How-to-batch-convert-multiplex-any-files-with-ffmpeg
hope it helped 🙏.
For giggles, here's solution in fish-shell:
for i in *.avi; ffmpeg -i "$i" (string split -r -m1 . $i)[1]".mp4"; end
Bash is terrible to me, so under Linux/Mac, I prefer Ruby script:
( find all the files in a folder and then convert it from rmvb/rm format to mp4 format )
# filename: run.rb
Dir['*'].each{ |rm_file|
next if rm_file.split('.').last == 'rb'
command = "ffmpeg -i '#{rm_file}' -c:v h264 -c:a aac '#{rm_file.split('.')[0]}.mp4'"
puts "== command: #{command}"
`#{command}`
}
and you can run it with: ruby run.rb
Alternative approach using fd command (repository):
cd directory
fd -d 1 mp3 -x ffmpeg -i {} {.}.wav
-d means depth
-x means execute
{.} path without file extension
I developed a python package for this case.
https://github.com/developer0hye/BatchedFFmpeg
You can easily install and use it.
pip install batchedffmpeg
batchedffmpeg * -i folder * output_file
This will create mp4 video from all the jpg files from current directory.
echo exec("ffmpeg -framerate 1/5 -i photo%d.jpg -r 25 -pix_fmt yuv420p output.mp4");
I'm using this one-liner in linux to convert files (usually H265) into something I can play on Kodi without issues:
for f in *.mkv; do ffmpeg -i "$f" -c:v libx264 -crf 28 -c:a aac -b:a 128k output.mkv; mv -f output.mkv "$f"; done
This converts to a temporary file and then replaces the original so the names remain the same after conversion.
I needed all the videos to use the same codec for merging purposes
so this conversion is mp4 to mp4
it's in zsh but should easily be convertible to bash
for S (*.mp4) { ffmpeg -i $S -c:v libx264 -r 30 new$S }
If you want a graphical interface to batch process with ffmpegX, try Quick Batcher. It's free and will take your last ffmpegX settings to convert files you drop into it.
Note that you can't drag-drop folders onto Quick Batcher. So select files and then put them through Quick Batcher.
Another simple solution that hasn't been suggested yet would be to use xargs:
ls *.avi | xargs -i -n1 ffmpeg -i {} "{}.mp4"
One minor pitfall is the awkward naming of output files (e.g. input.avi.mp4). A possible workaround for this might be:
ls *.avi | xargs -i -n1 bash -c "i={}; ffmpeg -i {} "\${i%.*}.mp4""
And for Windows, this does not work
FOR /F "tokens=*" %G IN ('dir /b *.flac') DO ffmpeg -i "%G" -acodec mp3 "%~nG.mp3"
even if I do double those %.
I would even suggest:
-acodec ***libmp3lame***
also:
FOR /F "tokens=*" %G IN ('dir /b *.flac') DO ffmpeg -i "%G" -acodec libmp3lame "%~nG.mp3"
This is what I use to batch convert avi to 1280x mp4
FOR /F "tokens=*" %%G IN ('dir /b *.avi') DO "D:\Downloads\ffmpeg.exe" -hide_banner -i "%%G" -threads 8 -acodec mp3 -b:a 128k -ac 2 -strict -2 -c:v libx264 -crf 23 -filter:v "scale=1280:-2,unsharp=5:5:1.0:5:5:0.0" -sws_flags lanczos -b:v 1024k -profile:v main -preset medium -tune film -async 1 -vsync 1 "%%~nG.mp4"
Works well as a cmd file, run it, the loop finds all avi files in that folder.
calls MY (change for yours) ffmpeg, passes input name, the settings are for rescaling up with sharpening. I probs don't need CRF and "-b:v 1024k"...
Output file is input file minus the extension, with mp4 as new ext.

Batch .SRT Rip Using FFMPEG

I have this command using ffmpeg
root#ubuntu-4cpu-8gb-sg-sin1:/home/jaac/torrents/rtorrent/dots# ffmpeg -i Title.NF.WEB-DL.DDP2.0.x264-Ao.mkv -map 0:7 indo16.srt
That will rip 1 sub (Indonesia region)
How to rip it in batch? I have 17 files in dots folder
Thanks
Run these commands:
cd /home/jaac/torrents/rtorrent/dots
for f in *.mkv; do ffmpeg -i "$f" -map 0:s:m:language:ind "${f%.*}.srt"; done
Adapted from How do you convert an entire directory with ffmpeg?
What the -map option is doing: 0:s:m:language:ind is input #0: subtitles:metadata:language:indonesian. Which means it chooses all subtitle streams from the input that have Indonesian language metadata.
If you get error:
Stream map '0:s:m:language:ind' matches no streams.
To ignore this, add a trailing '?' to the map.
You can ignore it. Just a message telling you there is no subtitle stream with Indonesian language metadata in that particular input.
You can use a for loop:
for f in *.mkv; do ffmpeg -i $f -vf subtitles="${f%.mkv}".srt "${f%.mp4}"_sub.mkv; done
Subtitules files must have the same name as videos.
Thanks guys for helping me out.
I use this command to batch rip the subtitle:
#!/bin/bash
cd /home/jaac/torrents/rtorrent/dots || exit
for f in *.mkv; do ffmpeg -i "$f" -map 0:5:s:m:language:id "${f%.*}.srt"; done
I use Shellcheck
Thanks #llogan and #m8factorial

ffmpeg concat two sources with optional audio

I want to concat two video files together by using ffmpeg. I already found this command:
ffmpeg -i INPUT1 -i INPUT2 -filter_complex \"[0:v][0:a][1:v][1:a]concat=n=2:v=1:a=1[v][a]\" -map \"[v]\" -map \"[a]\" OUTPUT
The Problem is, that INPUT2 not alway has Audio, so ffmpeg throws an invalid Input Stream.
I'm very inexperienced with ffmpeg and I'm not getting smart out of the documentation.
Is there a possible way that is decently understandable, possibly in one command?
Summed up: I want to concat two Video Files including Audio, but the second file may or may not has an audio layer.
Must match stream count & type
All segments to be concatenated must have the same number and type of streams. So if input1.mp4 has audio, and input2.mp4 does not, then you need to either omit the audio from input1.mp4 or add audio for input2.mp4. One method is to add silent audio with the anullsrc filter as shown in:
FFmpeg concatenation, no Audio in Final Output
Add black video to video with sound
How to concatenate videos in ffmpeg with different attributes?
Detecting audio
There is no option to automatically add missing audio, so you have to check the inputs with ffprobe to tell you if the inputs have audio or not:
Using ffprobe to check audio-only files
How can I know a certain file is a video or audio file?
You can then use the results in your preferred scripting/coding language to execute the appropriate command.
A simple approach for a video that has no audio would be to add audio (a null audio source):
ffmpeg -f lavfi \
-i anullsrc=channel_layout=stereo:sample_rate=44100 \
-i "videoWithoutAudio.mp4" \
-shortest -c:v copy -c:a aac "videoWithAudio.mp4"
If you want to go further here is my shell script that
concatenates multiple videos
of different size, aspect ratio and format and
adds silent audio to videos without audio
Set target size
targetWidth=1920
targetHeight=1080
create scale expression that scales up if necessary depending on source
slhck came up with this awesome scale expression for ffmpeg. No matter which size or aspect ratio you have, this expression scales and pads as needed.
smartScale="scale=iw*min($targetWidth/iw\,$targetHeight/ih):ih*min($targetWidth/iw\,$targetHeight/ih), pad=$targetWidth:$targetHeight:($targetWidth-iw*min($targetWidth/iw\,$targetHeight/ih))/2:($targetHeight-ih*min($targetWidth/iw\,$targetHeight/ih))/2:color=black, setdar=ratio=16/9, setfield=tff"
Make counter and set up variables
c=0
ffmpegInput=""
filter_complex_params_1=""
filter_complex_params_2=""
Go over every Video and do 2 things:
Detect no Audio
Identify videos with no audio
Add null audio to those videos
string ffmpeg arguments together
Add every video as an ffmpeg input
Expand ffmpegs filter_complex for every new input
You can put your files into a folder, add numbers prior to their filename to determine the order of the concatenation. Do not use spaces in filenames! "01_SummerVacation_2020.mkv"
for i in *
do
# Use file command to recognize the type of data contained in a file
bashFileCommand=$(file "$i")
# parts of the file command output for many files that are not a video
regexPattern="image|Image|IMAGE|bitmap|text|Text|TEXT|ocument|DOCUMENT|Microsoft|rchive|empty|directory"
# detect if file is a video or not
# if the file contains no string of the above regexPattern it is likely a video
if [[ ! $bashFileCommand =~ $regexPattern ]]; then
# Concatenation works only if all videos contain audio
# detect videos without audio using ffprobe
# store command arguments for ffprobe query in array: codec_type for audio
command_audioCodec_type=(ffprobe -v quiet -select_streams a:0 -show_entries stream=codec_type -of default=nw=1:nk=1 "$i")
# execute command in array and store result variables
result_audioCodec_type=$("${command_audioCodec_type[#]}" | tail -1)
if [[ ! $result_audioCodec_type =~ "audio" ]]; then
# Add dummy audio to video
# Since we cannot overwrite the source file we have to create a temporary file
# ffmpeg chooses the right format according to the extension. Therefore we add "tmp" in front of the filename. Otherwise we would mess up the extension.
ffmpeg -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 -i "$i" \
-shortest -c:v copy -c:a aac "tmp_${i}"
# remove original
rm "${i}"
# renaming new file to appear as the original
mv "tmp_${i}" "${i}"
fi
# iterate over all videos (e.g. 3) and add the parameters to the previous parameters:
# Scale videos to set size
# 1st iteration: [0:v]${smartScale}[scaled_v0];
# 2nd iteration: [0:v]${smartScale}[scaled_v0];[1:v]${smartScale}[scaled_v1];
# 3rd iteration: [0:v]${smartScale}[scaled_v0];[1:v]${smartScale}[scaled_v1];[3:v]${smartScale}[scaled_v2];
filter_complex_params_1="$filter_complex_params_1[${c}:v]${smartScale}[scaled_v${c}]; "
# Scale videos to set size
# 1st iteration: [scaled_v0][0:a]
# 2nd iteration: [scaled_v0][0:a][scaled_v1][1:a]
# 3rd iteration: [scaled_v0][0:a][scaled_v1][1:a][scaled_v2][2:a]
filter_complex_params_2="$filter_complex_params_2[scaled_v${c}][${c}:a]"
# Add inputs
# 1st iteration: -i video1.mp4
# 2nd iteration: -i video1.mp4 -i video2.mp4
# 3rd iteration: -i video1.mp4 -i video2.mp4 -i video3.mp4
ffmpegInput="$ffmpegInput -i ${i}"
# increment counter for filter
c=$((c+1))
fi
done
create the concat filter
# 3rd (last) iteration: concat=n=3:v=1:a=1[v][a]
filter_complex_params_3="concat=n=${c}:v=1:a=1[v][a]"
Combine all variables to the final ffmpeg command
# concatenate videos
ffmpeg ${ffmpegInput} -filter_complex "${filter_complex_params_1}${filter_complex_params_2}${filter_complex_params_3}" -map "[v]" -map "[a]" -c:v libx264 -crf 23 -c:a aac concatenated.mkv

Run a function from while read line

I'm trying to run a function from while read line, the function contains ffmpeg commands to marge two files. but for some reason it's running the first $line and than breaks from loop.
"$filesList" contains three lines. I'm not sure what's wrong, but i can confirm with echo "$OFILE" that opener function runs three times if I comment out the ffmpeg commands, and only once with ffmpeg commands.
opener(){
OFILE="$1"
echo "$OFILE"
ffmpeg -i $opener_path -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate1.ts
ffmpeg -i $OFILE -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate2.ts
ffmpeg -i "concat:intermediate1.ts|intermediate2.ts" -c copy -bsf:a aac_adtstoasc merge_$OFILE
mv merge_$OFILE $OFILE
rm intermediate1.ts intermediate2.ts
}
while read line; do
if [ -e "$line" ]; then
opener "$line"
fi
done <<< "$filesList"
It appears one of the ffmpeg commands is reading from standard input, which consumes the rest of the contents of $filesList before the next call to read. I'm not familiar with ffmpeg, but two possibilities:
Does -i require an argument? Your posted code doesn't set the value of opener_path, so its unquoted expansion would produce an empty string that is discarded by the shell.
How is concat:intermediate1.ts|intermediate2.ts interpreted by ffmpeg? Given the call to rm, it seems to produce a pair of files from an unknown source.

Resources