Skip to content

Instantly share code, notes, and snippets.

@toenuff
Created December 8, 2015 19:39
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 toenuff/0d20587984d73c83ac56 to your computer and use it in GitHub Desktop.
Save toenuff/0d20587984d73c83ac56 to your computer and use it in GitHub Desktop.
function Get-Periscope {
param(
[Parameter(Mandatory=$true, Position=0)]
[string] $Id,
[Parameter(Mandatory=$true)]
[ValidateScript({Test-Path $_})]
[string] $Path
)
if (!(test-path $Path)) {
mkdir $Path
}
$data = Invoke-RestMethod "https://api.periscope.tv/api/v2/getAccessPublic?broadcast_id=$id" -SessionVariable s
foreach ($c in $data.cookies) {
$s.cookies.add((new-object System.Net.Cookie -ArgumentList $c.Name, $c.Value, $c.Path, $c.domain))
}
$header = @{Host="replay.periscope.tv"}
$playlist_file = (join-path $Path playlist.m3u8)
Invoke-WebRequest -WebSession $s -Headers $header $data.replay_url -outfile $playlist_file
$files = @()
$jobs = 5
$baseurl = ($data.replay_url -replace '/playlist.m3u8$', '') + '/'
$content = gc $playlist_file
for ($i=0; $i -lt $content.length; $i++) {
$line = $content[$i]
write-progress -activity "Downloading Files" -status $line -percentcomplete (($i/$content.length)*100)
if ($line -match '^chunk_') {
$url = $baseurl + $line
$file = join-path $Path $line
$files += $file
# Should probably parallelize this and remove write-progress
Invoke-WebRequest -Websession $s -Headers $header $url -outfile $file
}
}
write-verbose "Assembling parts into movie.ts"
$final_file = join-path $Path "movie.ts"
$files |% {
get-content $_ -Enc Byte -Readcount 512
} |set-content $final_file -enc Byte
$final_file
}
# You can get ffmpeg from here:
# http://ffmpeg.zeranoe.com/builds/
#
function Convert-TsToMp4 {
[cmdletbinding(DefaultParameterSetName='None')]
param (
[Parameter(ParameterSetName='RotateRight')]
[switch] $RotateRight,
[Parameter(ParameterSetName='RotateLeft')]
[switch] $RotateLeft,
[Parameter(Mandatory=$true)]
[ValidateScript({Test-Path $_})]
[string] $ffmpegpath,
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[ValidateScript({Test-Path $_})]
[string] $Path,
[string] $OutFile = 'video.mp4'
)
write-verbose "Converting TS to MP4"
$basecmd = "$ffmpegpath -y -v error -i $Path -bsf:a aac_adtstoasc {0} $Outfile"
switch ($pscmdlet.parameterSetName) {
'RotateRight' {
invoke-expression ($basecmd -f '-acodec copy -vf "transpose=1" -crf 21')
break
}
'RotateLeft' {
invoke-expression ($basecmd -f '-acodec copy -vf "transpose=2" -crf 21')
break
}
default {
invoke-expression ($basecmd -f "-codec copy")
}
}
}
###### Script logic begins here
$currdir = ''
if ($MyInvocation.MyCommand.Path) {
$currdir = Split-Path $MyInvocation.MyCommand.Path
} else {
$currdir = $pwd -replace '^\S+::',''
}
$outdir = join-path $currdir "video"
if (!(Test-Path $outdir)) {mkdir $outdir}
$file = Get-Periscope "1MYxNWngyXwKw" -Path $outdir
$file |Convert-TsToMp4 -ffmpegpath (join-path $currdir "ffmpeg.exe") -Outfile (join-path $outdir "video.mp4") -RotateRight
start (join-path $outdir "video.mp4")
# Copyright (c) 2015 Tome Tanasovski
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment