Skip to content

Instantly share code, notes, and snippets.

View salamanders's full-sized avatar

Benjamin Hill salamanders

View GitHub Profile
// ~/Documents/kotlin/kotlinc/bin/kotlinc -script dedupe.kts -- -d /media/pi
import java.io.File
require(args.isNotEmpty()) { "kotlinc dedupe.kts [-d] [ROOT FILE PATH]" }
val delete: Boolean = args[0] == "-d"
val root: File = File(args.last()).canonicalFile
require(root.exists() && root.isDirectory) { "Root must exist and be a directory: '$root'" }
println("Starting in '$root'")
find ./ -iname '*.VOB' -exec bash -c 'ffmpeg -f mpeg -i "{}" -c:a copy -c:v libx264 -crf 18 "{}".mp4' \;
find ./ -iname '*.VOB.mp4' -exec bash -c 'scenedetect -i "{}" -o ./out detect-content -m 120 -t 40 split-video -c' \;
@salamanders
salamanders / test.kt
Created September 30, 2019 21:56
Kotlin Firestore API (Collection documentChanges) to Flow
@ExperimentalCoroutinesApi
fun CollectionReference.toFlow(): Flow<DocumentChange> = callbackFlow {
val snapshotListener = this@toFlow.addSnapshotListener { snapshots, e ->
if (e != null) {
logger.error { "${this@toFlow.path} listen:error:$e" }
cancel(CancellationException("Collection Listener Error in ${this@toFlow.path}", e))
return@addSnapshotListener
}
for (dc in snapshots!!.documentChanges) {
@salamanders
salamanders / split_videos.sh
Last active August 21, 2019 20:37
Split a video by scene
brew update && brew upgrade
pip install --upgrade pip
pip install scenedetect[opencv,progress_bar]
mkdir out
# Optionally tune the threshold, using the content_val column
scenedetect -i clip1.mov -o ./out --stats my_video.stats.csv detect-content
# Write the files
scenedetect -i clip1.mov [...-i clip2.mov] -o ./out detect-content split-video --copy
@salamanders
salamanders / DoubleExponentialSmoothing.kt
Created April 10, 2019 20:09
Double Exponential Smoothing in Kotlin
package info.benjaminhill.basicbot2
import java.util.concurrent.ThreadLocalRandom
/**
* From https://geekprompt.github.io/Java-implementation-for-Double-Exponential-Smoothing-for-time-series-with-linear-trend/
*
* Performs double exponential smoothing for given time series.
*
@salamanders
salamanders / Main.kt
Created March 31, 2019 02:52
Clipshow: Media (images and videos) to side-by-side movie
/**
* @author Benjamin Hill benjaminhill@gmail.com
*/
import mu.KotlinLogging
import net.coobird.thumbnailator.Thumbnails
import org.bytedeco.javacpp.avutil
import org.bytedeco.javacpp.avutil.av_log_set_level
import org.bytedeco.javacv.FFmpegFrameGrabber
import org.bytedeco.javacv.FFmpegFrameRecorder
import java.awt.image.BufferedImage
import java.awt.image.DataBufferByte
import java.awt.image.DataBufferInt
/** Lots of alpha blurs looks bad using regular images */
class ManualImage(private val width: Int, private val height: Int) {
private val red = IntArray(width * height)
private val green = IntArray(width * height)
private val blue = IntArray(width * height)
private var numAdded = 0
@salamanders
salamanders / speedup.sh
Last active June 25, 2023 22:45
Speed up video 64x with frame blending/averaging using ffmpeg
# Simple 4x speedup, force to 60fps.
/c/bin/ffmpeg/bin/ffmpeg.exe -r 60 -i thefile.mkv -vf "tblend=average,framestep=2,tblend=average,framestep=2,setpts=0.25*PTS" -r 60 -c:v mpeg4 -q:v 1 -an thefile_4x.mp4
# Force input to 60fps. blend 16, and pick 1 of each 16. (do we do extra work for the other 15?) Set PTS to 1/16th of "original". Encode very high quality.
ffmpeg -r 60 -i molt.mp4 \
-vf "crop=in_h:in_h,tmix=frames=16:weights='1',select='not(mod(n\,16))',setpts=0.0625*PTS" \
-c:v libx265 -crf 18 -an molt16.mp4
# I still like the original. Match the "-r NN" to the source frame rate.
@salamanders
salamanders / Ezcam.kt
Created August 14, 2018 20:55
Deconstructed Camera2
package info.benjaminhill.cameratest
import android.Manifest
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import android.graphics.ImageFormat
import android.graphics.SurfaceTexture
import android.hardware.camera2.*
@salamanders
salamanders / EZPermissionActivity.kt
Last active August 1, 2018 23:17
Hello World Timer Camera using CameraKit
package info.benjaminhill.snailfps
import android.content.pm.PackageManager
import android.os.Bundle
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.widget.Toast