Skip to content

Instantly share code, notes, and snippets.

View docPhil99's full-sized avatar

Phil docPhil99

  • University of Sussex
View GitHub Profile
@docPhil99
docPhil99 / pythonVideoCapture.md
Last active September 28, 2023 18:11
How to write lossless video in Python

OpenCV does a reasonable job of reading videos from file or webcams. It's simple and mostly works. When it comes to writing videos, it however leaves a lot to be desired. There is little control over the codecs and it is almost impossible to know which codecs are installed. It also wants to know things like the frame size at intailisation. This isn't always a problem, but if you don't know it yet it means you have to set up the video writer inside your main processing loop.

To make something as cross-platform compatible as possible it would be nice to use FFmpeg. There are a few python wrappers around, but as far as I can tell they are mainly used for transcoding type applications. One solution is run FFmpeg as a subprocess and set its input to accept a pipe. Then every video frame is passed through the pipe. You write this yourself, in fact it's only a few lines of code. However, the scikit-video package will do this for us, with some nice boilerplate to ma

@docPhil99
docPhil99 / macFFmpeg.md
Last active April 30, 2024 20:43
Mac webcam FFmpeg

#Capture and stream a webcam To capture using the iSight camera on a Mac, or infact any other webcam connected to the Mac, we can use FFmpeg. First get a list of the devices installed.

ffmpeg -f avfoundation -list_devices true -i "" 

This will list the aviable video and audio devices.

The below will capture at 30fps and the set video size to a file. ffmpeg -f avfoundation -framerate 30 -video_size 640x480 -i "0:none" out.avi

@docPhil99
docPhil99 / numpyPipe.md
Last active March 16, 2018 16:59
Piping numpy arrays to other processes

To pipe data from one process to another as a stream in python we need to pickle the object and pass it to the pipe stream. In this example I've used Numpy arrays but this could be applied to any object that can be pickled in Python. This took far too long to get working and I could find little information online on how to put it all together so here it is. This code is Python 3 only, I've only run this on a Mac.

I've used binary as the stream rather than text purley becuase of effiencies. Numpy arrays can get huge! This means readline() is not going to work. Instead, I send a single control byte , 1, for data and 0 for stop. This could be extended to include other control operations. I then send the length of the data as a 8 byte int, followed by the data itself.

simpleSend.py

@docPhil99
docPhil99 / build_squeezelite.md
Last active June 5, 2023 03:18
Setting up a headless linux music server
  1. Stop the old service if it is running with sudo service squeezelite stop
  2. Remove the old version if installed with sudo apt-get remove squeezelite
  3. Install its dependancies sudo apt-get install lsb-base libasound2 libavcodec57 libavcodec-extra57 libavformat57 libavutil55 libc6 libfaad2 libflac8 liblirc-client0 libmad0 libmpg123-0 libsoxr0 libvorbisfile3
  4. Create a directory to download the source files in your home drive mkdir sources; cd sources
  5. Clone the source code git clone https://github.com/ralph-irving/squeezelite.git
  6. cd squeezelite
  7. sudo apt-get install libavcodec-dev libavformat-dev
  8. sudo apt-get install libsoxr-lsr0 libsox-dev sox libsoxr-dev
  9. Build it with make all OPTS="-DDSD -DRESAMPLE -DFFMPEG"
  10. sudo cp squeezelite /usr/bin/. to copy the file to you path
@docPhil99
docPhil99 / pandoc_lectures.md
Last active February 12, 2019 10:56
Making lecture notes with pandoc
  1. Install a markdown editor. I have used Atom with the following packaged added
    • markdown-preview-plus to view your pdf as you edit. This isn't configured to render the beamer slides but it produced a normal pdf document as you type which is still handy.
    • highlight-bad-chars - highlights none acsii chars that cause latex a problem
    • pdf-view is also handy - it opens your created pdf files directly in Atom.
    • Language Markdown - for some md auto formatting
    • Optional tweeks for Atom: In the core package Autocomplete-plus I would suggest changing 'Keymap For Confirming A Suggestion' to 'tab always, enter when suggestion explicity selected'. The default tab and enter is fine for programming but doesn't work well when writting English.
  2. Install latex - there are lots of ways of doing this depending on your OS. Make sure you have beamer installed as well.
  3. Install pandoc. The repo versions might be really old, so I installed the latest from here.
  4. Install any templates
@docPhil99
docPhil99 / ffmpeg_stream.md
Last active August 25, 2020 09:11
Streaming FFmpeg and Python

This is running on Linux Mint

  1. Install ffmpeg
sudo apt-get install ffmpeg
  1. A simple test: open two terminals, in first run ffplay udp://127.0.0.1:23000 and in the second ffmpeg -i sample.mp4 -vcodec mpeg4 -f mpegts udp://127.0.0.1:23000 . This should play the video sample.mp4 although the quality is rather blocky.
@docPhil99
docPhil99 / opencv_qt_label.md
Last active April 13, 2024 15:48
How to display opencv video in pyqt apps

The code for this tutorial is here

Opencv provides are useful, but limited, method of building a GUI. A much more complete system could be acheived using pyqt. The question is, how do we display images. There are quite a few possible routes but perhaps the easiest is to use QLabel since it has a setPixmap function. Below is some code that creates two labels. It then creates a grey pixmap and displays it one of the labels. code: staticLabel1.py

from PyQt5.QtWidgets import QWidget, QApplication, QLabel, QVBoxLayout
from PyQt5.QtGui import QPixmap, QColor
import sys
@docPhil99
docPhil99 / git_cheat_sheet.md
Last active March 2, 2020 16:27
Some useful notes on git

Split directory into a new repo

  • clone the repo into a new directory
  • cd into it
  • git filter-branch --prune-empty --subdirectory-filter FOLDER-NAME BRANCH-NAME
    • where:
    • FOLDER-NAME: The folder within your project that you'd like to create a separate repository from.
    • BRANCH-NAME: The default branch for your current project, for example, master.
  • Update the remote git remote set-url origin https://github.com/USERNAME/NEW-REPOSITORY-NAME.git
  • Verify git remote -v
  • Push the changes git push -u origin BRANCH-NAME
@docPhil99
docPhil99 / arg_parse_demo.py
Last active November 1, 2023 20:20
Simple demo of argparse in python
"""Simple demo of argparse in python, see http://zetcode.com/python/argparse/"""
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('name') # positional argument, note no dash!
parser.add_argument('-n', type=int, required=True, help="the number") # Required argument, must be a int, eg. -n 4
parser.add_argument('-e', type=int, default=2, help="defines the value") # Optional argument with a default
parser.add_argument('-o', '--output', action='store_true', help="shows output") # a binary flag