Recursing through a directory and converting music
If you want to also loop in subfolders, you can use the globstar
shell optional behavior, see the Pattern Matching section of the reference manual and the Shopt Builtin section of the reference manual as so:
shopt -s globstar
for f in **/*.m4a; do ffmpeg -i "$f" -acodec libmp3lame -ab 320 "${f%.m4a}.mp3"; done
Using find
it's a bit trickier since you're using a Shell Parameter Expansion. Here's a possibility that will be 100% safe regarding files with spaces or other funny symbols in their name:
find . -name '*.m4a' -type f -exec bash -c 'ffmpeg -i "$0" -acodec libmp3lame -ab 320 "${0%.m4a}.mp3"' {} \;
This second possibility might be faster if you have a huge number of files, since bash globbing is known to be quite slow for huge number of files.
In the -exec
statement of find
, I'm using bash -c '...'
. In this case, every parameter given after the string to be executed will be set as the positional parameters, indexed from 0
, hence the $0
that appears in the code
ffmpeg -i "$0" -acodec libmp3lame -ab 320 "${0%.m4a}.mp3"
Hope this helps!
Using for loop to move files from subdirectories to parent directories
Small change. change
subs=ls $dir1
to
subs=`ls $dir1`
Notice the backquotes. Backquotes actually execute the bash command and return the result. If you issue echo $subs
after the line, you'll find that it correctly lists folder1
, folder2
.
Second small change is to remove double quotes in the mv
command. Change them to
mv $dir1/$i/*/* $dir1/$i
Double quotes take literal file names while removing quotes takes the recursive directory pattern.
After that, your initial for loop is indeed correct. It will move everything from sub1
and sub2
to folder1
etc.
How do I recursively list all files of type *.mp3 in a Windows .bat file?
Use the command DIR
:
dir /s/b *.mp3
The above command will search the current path and all of its children. To get more information on how to use this command, open a command window and type DIR /?
.
Enumerating MP3 files returns too many results
There were two issues with your code, the first was just a simply typo of $file.length
instead of $files.length
.
The second was this line:
$trackLengths.Add($shellfolder.GetDetailsOf($shellfile, 27))
Which needs to be this:
$trackLengths.Add($shellfolder.GetDetailsOf($shellfile, 27)) | Out-Null
As it was returning an output (probably the index of array item added) as well as adding the item to the array. When using a PowerShell function beware that the Return
statement does not define the only thing returned by the function, anything that writes output will also be returned.
How to list all the folders and its files in php
function find_all_files($dir)
{
$root = scandir($dir);
foreach($root as $value)
{
if($value === '.' || $value === '..') {continue;}
if(is_file("$dir/$value")) {$result[]="$dir/$value";continue;}
foreach(find_all_files("$dir/$value") as $value)
{
$result[]=$value;
}
}
return $result;
}
Unable to get result while returning value from the function in PHP
If you want to return a list of all files found and you are hoping to recursively navigate through folders and sub-folders there needs to be, with the above code, some means of storing details for all files found which can be returned from this function. Rather than attempt to manually recurse directories and maintain a list of files found you might consider using one of the native PHP recursiveIterator
type classes.
If the format of the songs remains in a constant format you might consider a simpler method to tokenize the filename - list
combined with explode
allow for very easy naming of these tokens and with one of these tokens you can perform the lookup to find the full name. A regEx
would be more flexible if the format were to vary between artists/directories - but the pattern below would need considerable refinement perhaps ;-)
# a slightly modified version of the original function
function singeractualname( $ssn ) {
switch( $ssn ){
case 'MC':return 'Miley Cyrus';
case 'RI':return 'Rihanna';
default:return 'Singer name not available !!!';
}
}
function outputFiles( $path, $song, $dir ){
$output=[];
if( file_exists( realpath( $path ) ) ){
$dirItr=new RecursiveDirectoryIterator( $path, RecursiveDirectoryIterator::KEY_AS_PATHNAME );
foreach( new RecursiveIteratorIterator( $dirItr, RecursiveIteratorIterator::CHILD_FIRST ) as $obj => $info ) {
if( $info->isFile() ){
$pttn=sprintf( '@%s-\w+\.\w+@i', $song );
preg_match( $pttn, $info->getFileName(), $col );
if( !empty( $col ) ){
foreach( $col as $file ){
$ext=pathinfo( $file, PATHINFO_EXTENSION );
list( $int, $cat, $code )=explode( '-', pathinfo( $file, PATHINFO_FILENAME ) );
$output[]=(object)[
'ext' => $ext,
'code' => $code,
'name' => singeractualname( $code ),
'file' => $dir . '/' . $info->getFileName(),
'index' => $int,
'category' => $cat
];
}
}
}
}
}
array_unshift( $output, (object)[
'ext' => 'html',
'code' => false,
'name' => 'Home',
'file' => '/homepage.html',
'index' => false,
'category' => false
]);
return $output;
}
$PlaylistName = 'ROCK';
$songNumber = 1;
$root='c:/wwwroot';
$dir='/sfx/mp3/tmp';
$path=$root . $dir;
$song=sprintf( '%s-%s', $songNumber, $PlaylistName );
$arr=outputFiles( $path, $song, $dir );
foreach( $arr as $obj ) {
printf(
'<a href="%1$s" title="%2$s">%2$s</a><br />',
$obj->file,
$obj->name
);
}
Given a similar directory and content
The output would be
<a href="/homepage.html" title="Home">Home</a><br />
<a href="/sfx/mp3/tmp/1-ROCK-ACDC.mp3" title="Singer name not available !!!">Singer name not available !!!</a><br />
<a href="/sfx/mp3/tmp/1-ROCK-MC.mp3" title="Miley Cyrus">Miley Cyrus</a><br />
<a href="/sfx/mp3/tmp/1-ROCK-RI.mp3" title="Rihanna">Rihanna</a><br />
How do you convert an entire directory with ffmpeg?
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
Related Topics
How to Know the Creation Time of a File in Ubuntu
Error: Could Not Locate the Bindings File. Tried: #56
Node.Js Child Processes Being Killed When Parent Dies
How to Set Java Classpath in Linux
How Is Pthread_Join Implemented
How to Embed Version Information into Shared Library and Binary
How to Call a Function (Defined in Shell Script) in a Perl Script
In Linux, How to Test Whether the Output of a Program Is Going to a Live Terminal or to a File
What Is the Use of "Echo || True"
Force Cmake to Use the Full Library Path
How to Grep for a Pattern in the Files in Tar Archive Without Filling Up Disk Space
Awk and Special Brackets Delimiters
How to Write Shell Command Within Pharo Smalltalk
Replace First Few Lines with First Few Lines from Other File
Strange File Permission in Docker Container (Question Marks on Permission Bit and User Bit)
How to Do Runtime Binding Based on CPU Capabilities on Linux