Saturday, April 24, 2021

How to concatenate videos with ffmpeg

This one works for me:

ffmpeg -i input1.mp4 -i input2.webm -i input3.mov \
-filter_complex "[0:v:0][0:a:0][1:v:0][1:a:0][2:v:0][2:a:0]concat=n=3:v=1:a=1[outv][outa]" \
-map "[outv]" -map "[outa]" output.mkv


Reference: https://trac.ffmpeg.org/wiki/Concatenate

It assumes the videos have the same size and TBR. To transpose a video when one is vertical and another is horizontal, see here.
When video TBRs are different, it may come up with the strange message:

"More than 1000 frames duplicated"

When this happens, try adding "-vsync 2" to the command. Reference: https://stackoverflow.com/questions/62822889/ffmpeg-filter-complex-concat-never-completes. It worked for me.

How to transpose a video with ffmpeg

ffmpeg -i input.mp4 -vf "transpose=1" output.mp4

or

ffmpeg -i input.mp4 -vf "transpose=clock" output.mp4

for one 90-deg clockwise rotation.


Reference: https://ostechnix.com/how-to-rotate-videos-using-ffmpeg-from-commandline/#:~:text=FFMpeg%20has%20a%20feature%20called,flip%20them%20vertically%20and%20horizontally.&text=Here%2C%20transpose%3D1%20parameter%20instructs,video%20by%2090%20degrees%20clockwise.

Monday, April 5, 2021

Combine image + audio file to make a video

Source: https://video.stackexchange.com/questions/5291/how-to-make-a-video-from-a-still-image-and-an-audio-file

I tried at least 5 commands from different sites, and only this one worked well enough for me - about 37x speed, waitable.

ffmpeg -loop 1 -f image2 -r 2 -i image.jpg -i audio.m4a -c:v libx264 -c:a copy -shortest video.mp4

Wednesday, February 3, 2021

How to prevent Automounting of Volumes (USB drives, optical discs, etc) upon connecting on Mac OS X

 https://discussions.apple.com/docs/DOC-7942

  1. diskutil info /Volumes/<volume that shouldn't be mounted>
  2. Get UUID from output from above step 1.
  3. sudo /etc/vifs
  4. Add this to the file:
    1. UUID=<LONG-UUID-LIKE-F77F-3F72-A6C2-26676F39B7CE> none hfs rw,noauto
  5. sudo automount -vc
Done! Worked for me!

Saturday, December 26, 2020

ffmpeg aac -> mp3 conversion

It seems that it isn't easy to handle aac/m4a files in sox anymore. Online help threads suggest using ffmpeg to convert it to mp3 instead:

 ffmpeg -i input.m4a -c:a libmp3lame -q:a 8 output.mp3

Reference link: https://askubuntu.com/questions/1200430/how-to-convert-m4a-audio-files-to-mp3-use-command-line

Wednesday, January 9, 2019

watch

Have you ever wanted to run some command periodically, usually to wait for some change in the result (like waiting for some ACL to be propagated, a price value to change, etc)?

Seek no more! Do you know the "watch" unix command?
watch -n 60 <command>
This will run <command> every 60 seconds! And you can inspect the output every time it does.

Its usage is quite simple. The help output is:

Usage:
 watch [options] command

Options:
  -b, --beep             beep if command has a non-zero exit
  -c, --color            interpret ANSI color and style sequences
  -d, --differences[=<permanent>]
                         highlight changes between updates
  -e, --errexit          exit if command has a non-zero exit
  -g, --chgexit          exit when output from command changes
  -n, --interval <secs>  seconds to wait between updates
  -p, --precise          attempt run command in precise intervals
  -t, --no-title         turn off header
  -x, --exec             pass command to exec instead of "sh -c"

 -h, --help     display this help and exit
 -v, --version  output version information and exit

Useful command to know!

Saturday, December 22, 2018

Noise removal (sox) and fixing video/audio merge (ffmpeg)

When you have some audio and it's noisy, remove the noise with this:

# Create background noise profile from mp3
/usr/bin/sox noise.mp3 -n noiseprof noise.prof

# Remove noise from mp3 using profile
/usr/bin/sox input.mp3 output.mp3 noisered noise.prof 0.21
Referral:
https://unix.stackexchange.com/questions/425853/how-to-clear-background-noises-with-sox

_____________________________________________________________________________
When you're merging audio and video with ffmpeg, and your video ends up silent, remember to map the video and audio streams from the two files explicitly:
ffmpeg -y -i merged.mp4 -i combined.mp3 -c:a aac -map 0:v -map 1:a output.mp4
Referral:
https://unix.stackexchange.com/questions/425853/how-to-clear-background-noises-with-sox

Tuesday, October 2, 2018

Thursday, October 5, 2017

Trim a video

...with ffmpeg!

ffmpeg -i movie.mp4 -ss 00:00:03 -t 8 -async 1 cut.mp4
Easy peasy. Trims a video from second 3 for a length of 8 seconds. The -async 1 is optional... maybe it speeds it up with parallel processing?

Source link. Thanks!

Saturday, March 5, 2016

Ghostscript JPG -> PDF

I'm not sure why I never posted this, but this is very useful when making documents from photos.

HOW TO CONVERT A JPG TO A PDF FILE:

gs \
 -sDEVICE=pdfwrite \
 -o outfile.pdf \
  /usr/local/share/ghostscript/8.71/lib/viewjpeg.ps \
 -c \(infile.jpg\) viewJPEG
Notice the \(...\) around the filename. They're important!

Also, the "/usr/local/share/ghostscript/8.71/lib/viewjpeg.ps" path varies, depending on the current installed version of ghostscript. Run the following to find your correct version:

find /usr/local/share/ghostscript -name viewjpeg.ps

Ref: http://stackoverflow.com/questions/4283245/using-ghostscript-to-convert-jpeg-to-pdf

Thursday, June 11, 2015

Command-line WiFi manipulation!

I found this blog post a guy made on manipulating WiFi from command line. It's self-explanatory, so I'll copy-paste it and link to it. Many thanks, Matt Crampton!!

ORIGINAL LINK

Turn off wifi on your macbook from the Mac OSX terminal command line:
networksetup -setairportpower en0 off
Turn on wifi on your macbook from the Mac OSX terminal command line:
networksetup -setairportpower en0 on
List available wifi networks from the Mac OSX terminal command line:
/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport scan
Join a wifi network from the Mac OSX terminal command line:
networksetup -setairportnetwork en0 WIFI_SSID_I_WANT_TO_JOIN WIFI_PASSWORD
Find your network interface name
networksetup -listallhardwareports

Tuesday, May 26, 2015

Tesseract

So you have a big PDF. Or a bunch of PDFs. And they're the kind that have text in image format, so the text is not searchable. Want to command-line OCR it? That's what tesseract allows. Awesome, right? Yeahhhh...

So, follow the steps on either of these sites to get it installed:

But the site with the most comprehensive information on the entire OCR process is:

The first two sites suggest using imagemagick to convert PDF pages to image files, but the third one suggests using ghostscript with special parameterization for optimal performance, duly justified. Ghostscript worked better for me.

The PDF->image command they recommend is:
  • gs -dSAFER -dQUIET -sDEVICE=png16m -dINTERPOLATE -dNumRenderingThreads=8 -dFirstPage=STARTPAGE -dLastPage=ENDPAGE -r300 -o image%03d.png -c 30000000 setvmthreshold -f mypdf.pdf
This tells ghostscript to convert pages in file mypdf.pdf from STARTPAGE to ENDPAGE into sequential 300dpi-resolution png files called imageXXX.png (where XXX is a 3-digit 0-padded number) using 8 simultaneous rendering threads, maximizing the amount of RAM used (to process faster). The resulting images, they find, are of good enough quality to submit to the tesseract OCR.

The actual OCR command is much simpler:
  • tesseract image001.png image001
This tells tesseract to recognize all text in image001.png, and to save the textual result into image001.txt. tesseract allows many other options, including recognition parameters and language, but I didn't really get into the details of those, so I'll omit them here.

So, follow these two steps, and you can OCR your own PDFs! Follow only the second one if your input is already in image (jpg/png/bmp) format.

Another link I found with more info on the subject:
And a list of available language models for the tesseract algorithm to use:

Ghostscript

Ghostscript is awesome. It's available on command line, it comes with Mac OS X by default, and it allows a nice range of PDF manipulations. I'd already written a post where PDF documents are joined together into a single one (See PDF Merge). Now for a slight upgrade, here's a way to grab only certain pages from a document:

gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER \
       -dFirstPage=22 -dLastPage=36 \
       -sOutputFile=outfile.pdf inputfile.pdf

This script will take pages 22-36 from inputfile.pdf and save them into the new file outfile.pdf. Together with PDF merging, this little technique allows us to mix and match PDF pages into new documents as we best see fit.

My thanks to this site for the great tip!

Friday, August 22, 2014

Convert Video into an Image Sequence

ffmpeg to the rescue again!!

How to convert a video into a sequence of images? Just type, with your custom parameters:

ffmpeg -i <input_video> -r <images_per_sec> %05d.png
  • <input_video> is, of course, your input video file.
  • <images_per_sec> is the image extraction rate from the video. (Sampling rate from the video, you might say). e.g. 1 would get one image per second.
  • %05d.png means "the image filenames should be five digits long, start at 00001, and have the .png extension (and format).

Honored source:

Thank you honored source!

Tuesday, July 8, 2014

Making GIFs

I'm back!

Almost 2 years later. That's ok. I found out how to make GIFs!

For this you will need:

  • imageMagick (sudo apt-get install imagemagick).
  • A bunch of JPGs.
Command!

convert -delay 42 -loop 0 *.jpg NewGif.gif


You can tweak with the -delay 42 for timing, with the -loop 0 for how many times it loops, and with the file names. Enjoy!

Saturday, October 6, 2012

.bashrc, .bash_profile

While I've learned some nice bash tricks during the past few months, I haven't really gotten to customizing it or really understanding how it works from the bottom up. .bashrc and .bash_profile, for example, are two files close to the boundary of my current understanding of bash's structure.

I don't know how they really work just yet, but as I looked for how to implement bash aliases, I bumped into these sample .bashrc and .bash_profile files, which seem to illustrate bash's ample functionality comprehensively. I'll be honest, I haven't really read even five lines of it, but it seems like a super useful bit of text. Here's the link:


The website seems to be the Linux Documentation project, so I doubt it'll be going away anytime soon. (I was considering copy-pasting its contents, but that seems a bit over-paranoid).

Key repeatssssssssssssssssssssssssssss

I bump into this occasionally, but when I do, it irks me. By default on my OS X, if I press and hold the "a" key, instead of getting a long line of a's, I get this little window:


which sucks. If I wanted to type รก, I would've known it in advance and have pressed the special Option-E + A, or configured my keyboard for Spanish-style input, or found it on a web page somewhere and plagiarized it for my shameless personal gain (as I often do for arcane characters like ☮ and  ). If I wanted to type something like

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

without having to press the "a" key 81 times. I'd much prefer to instead press the "a" key, hold it for 5 seconds, and be done with it. Yes, I could do it on Terminal, but it's slow to switch to another application every time I want a specific kind of keyboard input.

So today I was browsing around, looking for a way to be able to execute binaries on the current directory, from the Terminal, by typing only their names, and not having to type in "./" at the beginning of the line (you know what I'm talking about). I haven't yet found that, but I bumped into this page:

http://mac.tutsplus.com/tutorials/terminal/10-terminal-commands-that-every-mac-user-should-know/

which among other sometimes-useful tips, taught me how to disable that sucky little key-hold window for the Mac OS X GUI:

defaults write -g ApplePressAndHoldEnabled -bool false

To be honest, I have not tested it (it requires logging off and on again), but I see no reason why it shouldn't work. Enjoy!