Skip to content

Instantly share code, notes, and snippets.

@porjo
Last active August 15, 2018 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save porjo/78fa85623060beef4267afdd17484e52 to your computer and use it in GitHub Desktop.
Save porjo/78fa85623060beef4267afdd17484e52 to your computer and use it in GitHub Desktop.
ffmpeg recipes

Mux mp4 + subtitles into an MKV

ffmpeg -fflags +genpts -i infile.mp4 -f srt -i subtitles.srt \
-map 0:0 -map 0:1 -map 1:0 -c:v copy -c:a copy -c:s srt outfile.mkv

-fflags +genpts necessary if you get Can't write packet with unknown timestamp errors.

Extract part of video using start + end times

Transcode to lossless Flac and while applying peak normalisation to audio track

#!/bin/bash
#
# process tab delmited 'runsheet' file with format
#	output name	input name	start time	end time
#
# where start/end time are hh:mm:ss
#

tosecs() {
   date '+%s' --date="$1"
}

fromsecs() {
 ((h=${1}/3600))
 ((m=(${1}%3600)/60))
 ((s=${1}%60))
 printf "%02d:%02d:%02d\n" $h $m $s
}

IFS=$'\n'
for i in `cat runsheet`; do

	oname=`echo $i | cut -f1`
	iname=`echo $i | cut -f2 | tr -d "'"`
	start=`echo $i | cut -f3`
	end=`echo $i | cut -f4`

	to=""
	if [[ -n $end ]]; then
		t=$(( $(tosecs "$end") - $(tosecs "$start") ))
		to="-t $(fromsecs $t)"
	fi
	if [[ -n $start ]]; then
		ss="-ss $start"
	else
		ss="-ss 00:00:00"
	fi

	cmd="ffmpeg $ss $to -i \"../$iname\" -vcodec copy -acodec flac -filter:a loudnorm $oname.mkv"
	echo $cmd
	eval $cmd

	date
	echo
done

Record from microphone, encode to Opus and send to network socket

ffmpeg -f pulse -i default -acodec libopus -b:a 96000 -vbr on -compression_level 10 -f rtp rtp://127.0.0.1:1234

Play from RTP stream:

Create SDP file using details output from ffmpeg command.

v=0
o=- 0 0 IN IP4 127.0.0.1
s=No Name
c=IN IP4 127.0.0.1
t=0 0
a=tool:libavformat 58.12.100
m=audio 1234 RTP/AVP 97
b=AS:96
a=rtpmap:97 opus/48000/2
a=fmtp:97 sprop-stereo=1

Then pass SDP file to RTP client:

ffplay -i opus.sdp -protocol_whitelist file,udp,rtp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment