How do I use afconvert to convert all the files in a directory from wav to caf? - command-line

I have a directory with about 50 wav files that I need to convert to caf, because AudioServicesCreateSystemSoundID() returns an error for some of them (but not all).
Here's an example of the command I've used successfully for a single file:
afconvert -f caff -d LEI16#44100 -c 1 whistle.wav whistle.caf
How do I do this quickly - not one-by-one for each file?

Similar approach for bash: for i in *.wav; do afconvert -f caff -d LEI16#44100 -c 1 $i ${i%.wav}.caf; done

On Windows, use the %~ni syntax.
for %i in (*.wav) do afconvert -f caff -d LEI16#44100 -c 1 %i %~ni.caf

for file in *.wav; do afconvert -f caff -d LEI16#44100 -c 1 "$file"; done
Hit the Return key directly after done.
Simple :)

For the people who are using OSX and are a bit afraid of Terminal scripts I created a little application with Automator, this application converts the files you select.
Download here

found this:
##
## Shell script to batch convert all files in a directory to caf sound format for iPhone
## Place this shell script a directory with sound files and run it: 'sh converttocaf.sh'
## Any comments to 'support#ezone.com'
##
for f in *; do
if [ "$f" != "converttocaf.sh" ]
then
/usr/bin/afconvert -f caff -d LEI16 "$f"
echo "$f converted"
fi
done
Customize the afconvert options then save it as a text file called 'converttocaf.sh', place it in the directory of files you want to convert and run it from Terminal.
It works with files with spaces in their names.

If you are dealing with filenames that contain spaces on Linux, try the following code:
SAVEIFS=$IFS
IFS=$(echo -en "\n\b"); for i in *.wav; do afconvert -f caff -d LEI16#44100 -c 1 $i ${i%.wav}.caf; done

Python script on OSX. Default data format of the .caf is ima4. Default directory is '.'
Make a file called wav2caf.py, put it in your path, make it executable, fill it with:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import os
import argparse
import glob
def main():
# handle command line args
parser = argparse.ArgumentParser(description='A program that converts .wav to .caf files.', formatter_class=argparse.RawTextHelpFormatter)
parser.add_help = True
parser.add_argument('-f', '--folder', dest='folder', type=str, default='.', help='folder of files to convert')
parser.add_argument('-d', '--data', dest='data', type=str, default='ima4', help='data format of .caf')
args = parser.parse_args()
# process files in folder
os.chdir(args.folder)
for filename in glob.glob("*.wav"):
name, ext = os.path.splitext(filename)
command = 'afconvert -f caff -d ' + args.data + ' ' + filename + ' ' + name + '.caf'
os.system(command)
if __name__ == "__main__":
main()
Converts all .wav to .caf in current directory:
wav2caf.py
Converts all .wav to .caf in specified directory:
wav2caf.py -f Desktop/wavs/
Converts all .wav to .caf with data type 'aac ':
wav2caf.py -d 'aac '

Related

Trim mp4 files without encoding it again

I have a .mp4 video file, I need to trim it, however no matter how I do it, trimmed video is being encoded again which results in noisy video.
What I've tried:
Open video with Matlab, read frames and write only the frames that I want to have in trimmed video, I use 'MPEG-4' option.
Trim video using Windows Movie Maker.
Trim video using VirtualDub.
In first 2 scenarios original mp4 movie is encoded again after trimming it. I couldn't get mp4 files open in VirtualDub.
So what would be the easiest way to trim a video without re-encdong it?
You can do the split and re-encode in one command.
Create a text file, list.txt,
like this
file 'in.mp4'
inpoint 48.101
outpoint 67.459
file 'in.mp4'
inpoint 76.178
outpoint 86.399
file 'in.mp4'
inpoint 112.140
outpoint 125.031
then run,
ffmpeg -f concat -i list.txt -an -crf 18 out_merged.mp4
I've solved it with the following commands:
ffmpeg.exe -ss 48.101 -t 19.358 -i in.mp4 -an out_part1.mp4
ffmpeg.exe -ss 76.178 -t 10.221 -i in.mp4 -an out_part2.mp4
ffmpeg.exe -ss 112.140 -t 12.891 -i in.mp4 -an out_part3.mp4
ffmpeg -i out_part1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intrmdt1.ts
ffmpeg -i out_part2.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intrmdt2.ts
ffmpeg -i out_part3.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intrmdt3.ts
ffmpeg -i "concat:intrmdt1.ts|intrmdt2.ts|intrmdt3.ts" -c copy out_merged.mp4
And some explanation:
Giving -ss (start time) and -t (duration) options before -i (input) option avoids unnecessary decoding.
Not using -c copy provides transcoding hence result more precise cut (got this from here).
I used -an because I didn't need the audio, if you need audio just omit this option.
Before concatenating the resulting trimmed videos I needed to transcode them to mpeg transport streams, to achieve lossless concatenation (for more details you can see this link).

Error while in a script for converting a folder of jpg to png for mac

I have a folder with a lot of jpg files and I want to convert them all into png. The thing is that I want to keep the name of the files. Let's see if you can help me.
#! /bin/bash
unset i2
for i in ./drawable/*.jpg
do
$i2 = ${i%%.jpg}
sips -s format png $i --out Converted/$i2.png
done
Here is a corrected version of your code :
#! /bin/bash
#you don't need to unset variables in bash if you want to reuse them
#for your script to be more portable, I would rather use 'for IMG in ./*.jpg'
#that way, you could execute the script anywhere on your computer with any .jpg file.
#with your current syntax, the script will fail if there is no ./drawable folder
for IMG in ./drawable/*.jpg
do
IMG_BASENAME=$(basename $IMG) #this will convert './drawable/img.jpg' to 'img.jpg'
sips -s format png $IMG --out Converted/${IMG_BASENAME%%.jpg}.png
#again, if you want the script to be portable, I would suggest using a fixed output directory
#for instance : sips -s format png $IMG --out ~/Documents/Converted/${IMG_BASENAME%%.jpg}.png
done

How can I extract multiple .gz log files in command line

I have a years worth of log files that are all in .gz files. Is there a command I can use to extract these all at once into their current directory? I tried unzip *.gz but doesn't work. Any other suggestions?
shell sciprt?
#!/bin/ksh
TEMPFILE=tempksh_$$.tmp #create a file name
> $TEMPFILE #create a file w/ name
ls -l | grep '.*\.gz$' \ #make dynamic shell script
| awk '{printf "unzip %s;\n", $9;}' \ #with unzip cmd for each item
>> $TEMPFILE #and write to TEMPFILE
chmod 755 $TEMPFILE #give run permissions
./$TEMPFILE #and run it
rm -f $TEMPFILE #clean up
Untested but i think you get the idea....
Actually a little fiddling and gets far simpler...
set -A ARR *.gz;
for i in ${characters[#]}; do `unzip $i`; done;
unset ARR;
For googles sake, since it took me here, it's as simple as this:
gzip -dc access.log.*.gz > access.log
As noted in a comment, you want to use gunzip, not gzip. unzip is for .zip files. gzip is for .gz files. Two completely different formats.
gunzip *.gz
or:
gzip -d *.gz
That will delete the .gz files after successfully decompressing them. If you'd like to keep all of the original .gz files, then:
gzip -dk *.gz

How to compare the content of a tarball with a folder

How can I compare a tar file (already compressed) of the original folder with the original folder?
First I created archive file using
tar -kzcvf directory_name.zip directory_name
Then I tried to compare using
tar -diff -vf directory_name.zip directory_name
But it didn't work.
--compare (-d) is more handy for that.
tar --compare --file=archive-file.tar
works if archive-file.tar is in the directory it was created. To compare archive-file.tar against a remote target (eg if you have moved archive-file.tar to /some/where/) use the -C parameter:
tar --compare --file=archive-file.tar -C /some/where/
If you want to see tar working, use -v without -v only errors (missing files/folders) are reported.
Tipp: This works with compressed tar.bz/ tar.gz archives, too.
It should be --diff
Try this (without the last directory_name):
tar --diff -vf directory_name.zip
The problem is that the --diff command only looks for differences on the existing files among the tar file and the folder. So, if a new file is added to the folder, the diff command does not report this.
The method of pix is way slow for large compressed tar files, because it extracts each file individually. I use the tar --diff method loking for files with different modification time and extract and diff only these. The files are extracted into a folder base.orig where base is either the top level folder of the tar file or teh given comparison folder. This results in diffs including the date of the original file.
Here is the script:
#!/bin/bash
set -o nounset
# Print usage
if [ "$#" -lt 1 ] ; then
echo 'Diff a tar (or compressed tar) file with a folder'
echo 'difftar-folder.sh <tarfile> [<folder>] [strip]'
echo default for folder is .
echo default for strip is 0.
echo 'strip must be 0 or 1.'
exit 1
fi
# Parse parameters
tarfile=$1
if [ "$#" -ge 2 ] ; then
folder=$2
else
folder=.
fi
if [ "$#" -ge 3 ] ; then
strip=$3
else
strip=0
fi
# Get path prefix if --strip is used
if [ "$strip" -gt 0 ] ; then
prefix=`tar -t -f $tarfile | head -1`
else
prefix=
fi
# Original folder
if [ "$strip" -gt 0 ] ; then
orig=${prefix%/}.orig
elif [ "$folder" = "." ] ; then
orig=${tarfile##*/}
orig=./${orig%%.tar*}.orig
elif [ "$folder" = "" ] ; then
orig=${tarfile##*/}
orig=${orig%%.tar*}.orig
else
orig=$folder.orig
fi
echo $orig
mkdir -p "$orig"
# Make sure tar uses english output (for Mod time differs)
export LC_ALL=C
# Search all files with a deviating modification time using tar --diff
tar --diff -a -f "$tarfile" --strip $strip --directory "$folder" | grep "Mod time differs" | while read -r file ; do
# Substitute ': Mod time differs' with nothing
file=${file/: Mod time differs/}
# Check if file exists
if [ -f "$folder/$file" ] ; then
# Extract original file
tar -x -a -f "$tarfile" --strip $strip --directory "$orig" "$prefix$file"
# Compute diff
diff -u "$orig/$file" "$folder/$file"
fi
done
To ignore differences in some or all of the metadata (user, time, permissions), you can pipe the result to awk:
tar --compare --file=archive-file.tar -C /some/where/ | awk '!/Mode/ && !/Uid/ && !/Gid/ && !/time/'
That should output only the true differences between the tar and the directory /some/where/
I recently needed a better compare than what "tar --diff" produced so I made this short script:
#!/bin/bash
tar tf "$1" | while read ; do
if [ "${REPLY%/}" = "$REPLY" ] ; then
tar xOf "$1" "$REPLY" | diff -u - "$REPLY"
fi
done
The easy way is to write:
tar df file This compares the file with the current working directory, and tell us about if any of the files has been removed.
tar df file -C path/folder This compares the file with the folder.

Error converting wav to caf for the iPhone using afconvert

Here's the command I'm typing in a Terminal window:
afconvert -f caff -d LEI16#44100 -c 1 buzzer.wav buzzer.caf
Error: ExtAudioFileOpenURL failed ('dta?')
Notice that buzzer.wav is my sound file.
What's wrong? How should I do the conversion?
Not all wave (.wav) files are alike. Some are actually some unrecognized file format. What fixed it for me was to open the file in Audacity and then export the sound in wave format; then use afconvert.