OSX equivalent of piping sound to linux's aplay - macos

On Ubuntu I am able to use aplay to play sound generated live from a script by piping the output of my script to aplay's stdin :
./generate_sound.py | aplay -r 2000 -c2 -f MU_LAW
cat sample.wav | aplay
Is there a way to do the same from terminal in OSX? I think afplay doesn't support this ...
Maybe someone knows another OSX command line sound player that would do the trick?

I had high hopes for redirection/piping, but afplay /dev/stdin <<< $(generate_sound.py) failed for all the formats I tried. Sadly afplay doesn't let you specify the format, and so it tries instead to sniff it which probably involves seeking which doesn't work with pipes.
I think you'd better find another command line player. sox seems like a good candidate. And! It's installable via homebrew: brew install sox and you can pipe data to it like so:
cat whatever.raw | play -t raw -e floating-point -b 32 -c 2 -r 44100 -

To Listen to an FM station on a mac
rtl_fm -f 95.3e6 -M wbfm -s 200000 -r 48000 – | aplay -r 48k -f S16_LE
To record for 10s
export AUDIOSAMPLERATE=48000
export SAMPLERATE=200000
export FREQ="127.2m"
rtl_fm -f $FREQ -M am -s $SAMPLERATE -r $AUDIOSAMPLERATE | sox -r $AUDIOSAMPLERATE -t raw -e s -b 16 -c 1 -V1 - FILENAME.wav&
sleep 10
killall rtl_fm

Related

How to create random pass names in ffmpeg?

Normally, when using ffmpeg, you define a name for the "pass" information to be stored in a file when enconding videos with 2 (or more) passes, e.g.:
ffmpeg -i "INPUT_FILE" -pass 1 -passlogfile videopass.log /dev/null -y && ffmpeg -i "INPUT_FILE" -pass 2 -passlogfile videopass.log -y "OUTPUT_FILE"
I'd love to be able to create a random pass name automatically in bash in the simplest way possible, preferably a "one-liner" using the system's default tools (Linux)... something like:
$(tr -dc A-Za-z0-9 </dev/urandom | head -c 8).log | ffmpeg -i "INPUT_FILE" -pass 1 -passlogfile > /dev/null -y && ffmpeg -i "INPUT_FILE" -pass 2 -passlogfile > -y "OUTPUT_FILE"
I'm too stupid to figure out a way of doing something similar that actually makes sense.
Thank you very much in advance!
Assuming the requirement is to create a random name for the password file:
$ pwdlog=$(mktemp XXXXXXXX.log) # have mktemp create a file with 8 random characters + ".log"
$ typeset -p pwdlog
declare -- pwdlog="Em6GeMdc.log"
$ ls -l "${pwdlog}"
-rw-------+ 1 username None 0 Apr 26 15:13 Em6GeMdc.log
This file could then be referenced in the ffmpeg call like such:
ffmpeg -i "INPUT_FILE" -pass 1 -passlogfile "${pwdlog}" ...
After countless tries and googling, I found a way to do exactly what I was looking for... A one-liner, nothing too complicated or fancy and using my system's default tools:
printf $RANDOM | xargs -i sh -c 'ffmpeg -i "INPUT_VIDEO" -pass 1 -passlogfile {} -an -f mp4 /dev/null -y && ffmpeg -i "INPUT_VIDEO" -pass 2 -passlogfile {} -y "OUTPUT_VIDEO"'
This will create a very simple 5-digit random number, which will be used as the initial name for the pass files in ffmpeg, ffmpeg automatically adds the file extension(s). Of course, you can (and probably should) add video/audio parameters to your ffmpeg commands. The important elements are the "printf $RANDOM | xargs -i sh -c" at the begining and the "{}" curly brackets after the "-passlogfile" command.
There are probably even simpler or more elegant ways of doing it, but this works exactly as I wanted, so I'm happy.

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.

Fish shell input redirection from subshell output

When I want to run Wireshark locally to display a packet capture running on another machine, this works on bash, using input redirection from the output of a subshell:
wireshark -k -i <(ssh user#machine "sudo dumpcap -P -w - -f '<filter>' -i eth0")
From what I could find, the syntax for similar behavior on the fish shell is the same but when I run that command on fish, I get the Wireshark output on the terminal but can't see the Wireshark window.
Is there something I'm missing?
What you're using there in bash is process substitution (the <() syntax). It is a bash specific syntax (although zsh adopted this same syntax along with its own =()).
fish does have process substitution under a different syntax ((process | psub)). For example:
wireshark -k -i (ssh user#machine "sudo dumpcap -P -w - -f '<filter>' -i eth0" | psub)
bash | equivalent in fish
----------- | ------------------
cat <(ls) | cat (ls|psub)
ls > >(cat) | N/A (need to find a way to use a pipe, e.g. ls|cat)
The fish equivalent of <() isn't well suited to this use case. Is there some reason you can't use this simpler and more portable formulation?
ssh user#machine "sudo dumpcap -P -w - -f '<filter>' -i eth0" | wireshark -k -i -

Is it possible to run two programs simultaneously or one after another using a bash or expect script?

I have basically two lines of code which are:
tcpdump -i eth0 -s 65535 -w - >/tmp/Captures
tshark -i /tmp/Captures -T pdml >results.xml
if I run them both in separate terminals it works fine.
However I've been trying to create a simple bash script that will execute them at the same time, but have had no luck. Bash script is as follows:
#! /bin/bash
tcpdump -i eth0 -s 65535 -w - >/tmp/Captures &
tshark -i /tmp/Captures -T pdml >results.xml &
If anyone could possibly help in getting this to work or getting it to "run tcpdump until a key is pressed, then run tshark. then when a key is pressed again close."
I have only a little bash scripting experience.
Do you need to run tcpdump and tshark separately? Using a pipe command will feed the output of tcpdump to the input of tshark.
tcpdump -i eth0 -s 65535 | tshark -T -pdml > results.xml

#/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