moshpit

package module
v1.0.1-0...-6923bf7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 10, 2020 License: MIT Imports: 17 Imported by: 0

README

moshpit

A command-line tool for surgical I-Frame removal, so-called datamoshing.
Comes with built-in scene cut detection for optimal results.

tutorial original moshed

Table of contents

Installation

Aside from the moshpit binary, which can be downloaded from the releases page, you need a copy of FFmpeg installed on your machine.

Usage

Arguments
moshpit [options] <file>

moshpit takes the video file you want to mosh as the last argument.

Option Description Default
-ffmpeg Specifies the location of the FFmpeg binary. ffmpeg
-log Specifies the target location of the FFmpeg log file. no logging
Commands

After starting moshpit, you can use the following commands to create a datamoshed video:

scenes

scenes <threshold>

Datamoshing via I-Frame removal yields the best results when applied at scene cuts. The scenes command finds scene cuts in the input file, using the threshold parameter to determine the similarity of each frame with the preceding frame.

A threshold of 0.2 usually gives good results.

mosh

mosh <output> <frame> [frame...]

Moshes the input file, writing it to the specified output file.
I-Frame removal is performed at the given frame indices, with scene cuts previously detected using the scenes command being suggested.
Using all as a frame parameter performs I-Frame removal at all previously detected scene cuts.

exit

Exits moshpit.
Moshpit can also be terminated at any time using Ctrl+C (SIGINT).

How it works

The theory behind datamoshing

Source

Modern compressed video files have very complex methods of reducing the amount of storage or bandwidth needed to display the video. To do this most formats, such as the AVI format, don't store the entire image for each frame.

Frames which store an entire picture are called I-frames (Intra-coded), and can be displayed without any additional information.

Frames which don’t contain the entire picture require information from other frames in order to be displayed, either previous or subsequent frames, these frames are called P-frames (Predicted) and B-frames (Bi-predictive). Instead of storing full pictures, these P-frames and B-frames contain data describing only the differences in the picture from the preceding frame, and/or from the next frame, this data is much smaller compared to storing the entire picture — especially in videos where there isn’t much movement.

When a video is encoded, or compressed, a combination of these types of frames are used. In most cases this means many P-frames with I-frames interspersed at regular intervals and where drastic visual changes in the video occur. More information on frame types can be found here.

If an I-frame is corrupted, removed or replaced, the data contained in the following P-frames is applied to the wrong picture. In the above video I-frames have been removed and so instead of scenes changing properly you see the motion from a new scene applied to a picture from a previous frame. This process of corrupting, removing or replacing I-frames is the video datamoshing technique that moshpit uses.

What moshpit does

When running the mosh command, moshpit converts the input file into an AVI file, placing I-Frames only at the frames specified by the user. This is done because single frames can be very easily identified and changed in the AVI format.

Each of the I-Frames in the resulting AVI file is then replaced with the next P-Frame, which means that the moshed video has the same duration as the original video, as opposed to removing the I-Frames, which would cause the moshed video to be shorter.

Finally, the moshed AVI file is "baked", which means it's converted back into an MP4 file, persisting the artifacts in the AVI file into a stable video file.

Building from source

go get github.com/CrushedPixel/moshpit/cmd/moshpit

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnalyzeFrames

func AnalyzeFrames(ctx context.Context, inputFile io.Reader,
	framesChan chan<- FrameType, errorChan chan<- error)

AnalyzeFrames analyzes the frames in the given file, writing results to the channel provided. The file is assumed to have AVI format. Any errors encountered are sent to the error channel. The error channel is closed when processing is finished.

func AviScanner

func AviScanner(reader io.Reader) *bufio.Scanner

AviScanner returns a Scanner that reads an AVI file frame-by-frame.

func ConvertToAvi

func ConvertToAvi(ctx context.Context, ffmpegPath string,
	ffmpegLogPath string, inputFile string, outputFile string, quality float64,
	iFrameIndices []uint64, progressChan chan<- float64,
	errorChan chan<- error)

ConvertToAvi uses ffmpeg to convert the input file into a mute AVI file for datamoshing. The encoding quality of the output file is determined by the quality parameter, with 0.0 being the lowest and 1.0 being highest possible quality setting. If iFrameIndices is not nil, automatic I-Frame generation is disabled and I-Frames are placed at the given frame indices. The encoding progress is frequently written to the progress channel as a value between 0.0 and 1.0. The error channel is closed when processing is finished.

func ConvertToMp4

func ConvertToMp4(ctx context.Context, ffmpegPath string,
	ffmpegLogPath string, aviFile string, soundFile string,
	outputFile string, quality float64,
	progressChan chan<- float64, errorChan chan<- error)

ConvertToMp4 uses ffmpeg to convert the input file into an MP4 file, taking the audio stream from another file. If the sound file path is empty, no audio is added to the output file. The encoding quality of the output file is determined by the quality parameter, with 0.0 being the lowest and 1.0 being highest possible quality setting. The encoding progress is frequently written to the progress channel as a value between 0.0 and 1.0. Any errors encountered are sent to the error channel. The error channel is closed when processing is finished.

func FindScenes

func FindScenes(ctx context.Context, ffmpegPath string,
	ffmpegLogPath string, inputFile string, threshold float64,
	sceneTimeChan chan<- VideoTime, progressChan chan<- float64,
	errorChan chan<- error)

FindScenes uses ffmpeg to find scene changes in the input file, using the given similarity threshold between 0 and 1. The detection progress is frequently written to the progress channel as a value between 0.0 and 1.0. Any errors encountered are sent to the error channel. The error channel is closed when processing is finished.

func RemoveFrames

func RemoveFrames(ctx context.Context, input io.Reader, output io.Writer,
	framesToRemove []uint64, processedChan chan<- uint64, errorChan chan<- error)

RemoveFrames writes a copy of the AVI data from the input reader to the output writer, replacing the frames at the given indices with the following frame. Any errors encountered are sent to the error channel. The error channel is closed when processing is finished.

Types

type FrameType

type FrameType uint
const (
	Unknown FrameType = iota
	IFrame
	PFrame
)

type VideoTime

type VideoTime struct {
	Time  time.Duration
	Frame uint64
	Fps   float64
	// contains filtered or unexported fields
}

func (*VideoTime) Timecode

func (v *VideoTime) Timecode() string

Timecode returns the VideoTime formatted as a Timecode.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL