chrippa / livestreamer

Command-line utility that extracts streams from various services and pipes them into a video player of choice. No longer maintained, use streamlink or youtube-dl instead.
http://livestreamer.io/
BSD 2-Clause "Simplified" License
3.88k stars 585 forks source link

How to record in 1 hour segments? #1558

Open iamWALRUS77 opened 7 years ago

iamWALRUS77 commented 7 years ago

I'm trying to record a very long livestream in 1 hour segments is this possible through live streamer?

My initial workflow I used youtube-dl to grab the manifest and fed that into ffmpeg to record in segments, but after a couple hours the manifest expired and the capture died.... is it possible to pipe livestreamer directly into ffmpeg somehow?

skulblakka commented 7 years ago

Take a look at #303 and #812

iamWALRUS77 commented 7 years ago

Thanks 303 is definitely helping. Someone posted this:

livestreamer -a="" -p="ffmpeg -i - af.flv" -v [streamurl] best

And it seemed to almost work... I'm trying got save as source and not transcode this so I tried changing the extension to mp4, but nothing happens...

Here's the script I was using initially with ffmpeg and the .m3u8

iamWALRUS77 commented 7 years ago

ffmpeg -i [playlist/index.m3u8] -bsf:a aac_adtstoasc -c copy -flags +global_header -f segment -segment_time 60 -segment_format_options movflags=+faststart -reset_timestamps 1 test%d.mp4

I'm trying to figure out how to somehow combine these scripts? I'm pretty out of my element since scripts/coding are definitely not my wheelhouse. Sorry if these questions come across as ridiculous!

iamWALRUS77 commented 7 years ago

Also for further context, the livestream I'm working on is supposed to last about 30 days non stop and I'm trying to record this offsite via the youtube live link as an easily accessible backup (for promos, highlights, etc) since our local backups are inaccessible due to bandwidth issues and the remote location of the stream.

Ideally it'll just run in the background for a month without needing to be babysat. Once I get this running I'll I have to work on getting the recording to power through the livestream connection dropouts that seem to happen once every couple hours.

Does my approach seem sound? I'm just sort of figuring this out as I go, so I may be overcomplicating this.

skulblakka commented 7 years ago

I honestly don't know much about ffmpeg.

If you want to repeatedly start livestreamer when it disconnects, you can also take a look at the scripts posted in #1244. In theory you could also extend the script a little bit to kill livestreamer after an hour . This way you could create one hour long segments without even having to use ffmpeg (you would however loose a little bit of video while livestreamer reconnects).

iamWALRUS77 commented 7 years ago

Interesting! I'm going to defintely look into this. If I can avoid going through FFMPEG that'll simplify things quite a bit.

So I should look into making the script kill the connection after an hour where it will then automatically reconnect? Or would I need to make livestreamer completely stop and restart somehow?

skulblakka commented 7 years ago

Changing the last part (above the goto) of the script to something like the following might work:

start "livestreamer-%fullstamp%" livestreamer twitch.tv/monstercat best -o "S:\monstercat-%fullstamp%"
timeout /t 3600
taskkill /FI "WINDOWTITLE eq livestreamer-%fullstamp%"

This will start livestreamer and then send a terminate request to livestreamer after 3600 seconds.

iamWALRUS77 commented 7 years ago

Awesome!! .... dumb question... how do I run this script on a mac? Do I make a .bat use a virtual machine or can this be done natively?

skulblakka commented 7 years ago

On Mac you need a new script that does the same thing. You can try the following script:

#!/bin/bash
trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT
while :
do
    streamlink twitch.tv/monstercat best -o "cohhcarnage-$(date "+%Y-%m-%d_%H%M%S")" & pid=$!
    (sleep 30 && kill -9 $pid)
done

I don't have a Mac so I can't test it myself.

Edit: Something was lost during copy & paste. Fixed it.

iamWALRUS77 commented 7 years ago

How do I execute this on a mac? I tried copying it into a text document, changed the extension to .sh, and then opened with terminal, but it didn't seem to work.

Post-Pit-2:~ john.post$ /Users/john.post/Desktop/stream.sh ; exit;
/Users/john.post/Desktop/stream.sh: line 1: {rtf1ansiansicpg1252cocoartf1404cocoasubrtf470: command not found
/Users/john.post/Desktop/stream.sh: line 2: syntax error near unexpected token `}'
/Users/john.post/Desktop/stream.sh: line 2: `{\fonttbl\f0\fmodern\fcharset0 Courier;\f1\fnil\fcharset0 Consolas;}'
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.

[Process completed]
skulblakka commented 7 years ago

Like I said above I don't have a Mac. The file above works on my linux machine and at least in theory run on mac too. Maybe the text editor you used added additional characters to the file that shouldn't be in a bash script?

iamWALRUS77 commented 7 years ago

THANK YOU!! You were totally right. When I made the text document it was an RTF and added a bunch of hidden characters. Changed it to plain text and the syntax errors from the hidden characters are gone

After running the script I had a couple unmatched pair errors for missing ") characters. I don't really know the syntax so I ended up just adding and dropping ") on line 5 until the script finally started running.

Right now it's recording and I just have to wait and see if it'll recover from a dropped connection and successfully split the files after an hour.

Thanks again!

iamWALRUS77 commented 7 years ago

I ended up just changing 3600 to 60 to see how it'd handle making new files every minute instead of an hour for test purposes, but it seems like it's not terminating after the designated time. Here's the script for reference.


#!/bin/bash
trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT
while :
do
    livestreamer https://www.youtube.com/watch?v=TSSEqdRFjbE best -o SOLITARY-$(date +%Y-%m-%d_%H%M).MP4
    (sleep 60 && kill -9 $pid)

done  

Also with this script will it restart after terminating or do I need to add to this?

flijloku commented 7 years ago

If I understand correctly what about something like: @ @echo off ::Копируем ссылку на VOD в буфер обмена и запускаем бат-файл ::Берем значение из буфера и записываем в файл nircmd.exe clipboard addfile "info1.txt" ::Читаем первую строку файла set /p param=<"info1.txt" del info1.txt Echo Soderzimoe bufera obmena: %param%

E: cd E:\VODs

:loop ::Ищем прямую ссылку для скачивания youtube-dl -g "%param%" > 1.txt set /p playlist=<"1.txt" del 1.txt

::Удаляем лишние символы в названии вода set paramready=%param:/=% set paramready=%paramready:?=% set paramready=%paramready::=% set paramready=%paramready:.=% set paramready=%paramready:https=%

::Вставляем дату и время в название вода set h=%TIME:~0,2% set m=%TIME:~3,2% set s=%TIME:~6,2% set curtime=%h%-%m%-%s% ::Заменяем на 0 пробел возникающий если время меньше 10 часов set curtime=%curtime: =0% set dd=%DATE:~0,2% set mm=%DATE:~3,2% set yyyy=%DATE:~6,4% set curdate=%yyyy%-%mm%-%dd% set curdatetime=%curdate%_%curtime% ::Формируем имя вода set vodname=%curdatetime%%paramready%

ffmpeg -i "%playlist%" -ss 00:00:00 -t 00:01:00 -c copy %vodname%.mkv goto loop pause

"nircmd.exe" to assign variable values from the clipboard (just copy the stream url to the clipboard and run the bat file). "-c copy" and "mkv" for save without reencoding from many formats.

skulblakka commented 7 years ago

@flijloku Your script won't work for him since he is using a Mac

@iamWALRUS77 I made a mistake when copy and pasting the script to github yesterday. While I fixed it pretty quick it looks like you were faster copying it^^ What you are missing is the last part in the livestreamer line which is also the reason why you had issues with missing brackets. (You are basically missing the %s for the seconds in the filename and the & pid=! at the end of line 5)

Also with this script will it restart after terminating or do I need to add to this?

Yes, thats what the do while does. The script will repeat until you press CTRL-C to terminate it.

flijloku commented 7 years ago

@skulblakka this is an example. In other operating systems, you can do it in their own way. The basic is the command for ffmpeg.

dajoka commented 7 years ago

hi,

I am a command prompt beginner.

how can I achieve the below with an hlsvariant stream? I want to break recording into multiple files, too.

start "livestreamer-%fullstamp%" livestreamer twitch.tv/monstercat best -o "S:\monstercat-%fullstamp%"
timeout /t 3600
taskkill /FI "WINDOWTITLE eq livestreamer-%fullstamp%"

I usually just open command prompt in windows as administrator, cd to livestreamer.exe location, then do: livestreamer "hlsvariant://http://xxxxxxxxxxxxxxx.org/stream.m3u8" best -o movie.mp4

the url expiresquickly so i have to do things quickly.

I want to avoid ffmpeg.

thank you!

flijloku commented 7 years ago

I think you need to create a new issue. So a better chance of being seen. Also probably better to create issue here: https://github.com/streamlink/streamlink/issues , as livestreamer is abandoned.

skulblakka commented 7 years ago

@dajoka I don't exactly understand your issue. You can simply change the URL twitch.tv/monstercat to whatever URL you want to use. Works the same as with the command prompt.

flijloku commented 7 years ago

@dajoka If url expires quickly how you get a new url ?

dajoka commented 7 years ago

I tried to use the script but I can't get it to work. I get something like a countdown, and it says "press any key to continue." But no file is written...

As far as link expiring, that's what I understand I need to do. I copy URL with urlsnooper, stop playback in browser, quickly paste in command prompt. It works that way. It just writes a big file and I want smaller files...

I'd appreciate any help .

flijloku commented 7 years ago

So you create a bat-file? If so, show it here. If not, then you need to do it. It would also be good to look at the video source.

dajoka commented 7 years ago

thanks for trying to help me. Yes, i created a bat file. here's the code:

start "livestreamer-%fullstamp%" livestreamer "hlsvariant://http://content.uplynk.com/d0901439.m3u8?8c6942" 504p -o "C:\ncaa-%fullstamp%"
timeout /t 40
taskkill /FI "WINDOWTITLE eq livestreamer-%fullstamp%"

i shortened the long string of characters in the url. thank you again.

dajoka commented 7 years ago

then i place the bat file in the directory where livestreamer.exe resides and I double-click it.

the stream is from a well known sports broadcaster, you know what I'm talking about.

skulblakka commented 7 years ago

Is that the full file? The snippet is intended to be an extension to another script (see the thread I referenced before, basically you are missing the loop and the calculation of the timestamp).

However that snipped should still run livestreamer once. What is happening if you run the bat file? Whats the output the command prompt is showing?

dajoka commented 7 years ago

yes, that's the full file. sorry, I didn't know that was a snippet of a larger script.

give me a few minutes. I'll snag a screen record.

dajoka commented 7 years ago

when i recorded my screen I was able to see what was output by the command prompt:

unable to open url: hlsvariant://http://long.url.m3u8?anotherlongstreamofcharacters (400 client error: bad request)

The same link type works with command prompt: running the below at a command prompt works and records live stream and saves the .ts file in the same dir as livestreamer.exe:

c:\Program Files (x86)\Livestreamer>livestreamer "hlsvariant://http://long.url.m3u8?anotherlongstreamofcharacters" 504p -o ncaa.ts
[cli][info] Found matching plugin stream for URL hlsvariant://http://long.url.m3u8?anotherlongstreamofcharacters
[cli][info] Available streams: 180p (worst), 288p, 432p, 504p, 720p (best)
[cli][info] Opening stream: 504p (hls)
[download][ncaa.ts] Written 14.7 MB (1m11s @ 118.4 KB/s)                                                                                                                                                                                                             Interrupted! Closing currently open stream...

c:\Program Files (x86)\Livestreamer>

I just typed Control+C and interrupted the process. I successfully opened the small .ts file and watched the video.

dajoka commented 7 years ago

sorry, in the first line in previous post I meant to say, what was output by running the .bat file.

flijloku commented 7 years ago

I try

@echo
set fullstamp=QWERTY
echo %fullstamp%
start "livestreamer-%fullstamp%" livestreamer "hlsvariant://http://hls.goodgame.ru/hls/7692.m3u8" best -o "C:\ncaa-%fullstamp%"
timeout /t 40
taskkill /FI "WINDOWTITLE eq livestreamer-%fullstamp%"

and also not get the desired result. So I can not help. But if you want, you can use the BAT-file with FFmpeg slightly modifying it.

flijloku commented 7 years ago

Helped replacement "hlsvariant" to "hls" and instead VAR %fullstamp% put my code:

@echo off

:loop
set h=%TIME:~0,2%
set m=%TIME:~3,2%
set s=%TIME:~6,2%
set curtime=%h%-%m%-%s%
set curtime=%curtime: =0%
set dd=%DATE:~0,2%
set mm=%DATE:~3,2%
set yyyy=%DATE:~6,4%
set curdate=%yyyy%-%mm%-%dd%
set curdatetime=%curdate%__%curtime%

start "livestreamer-%curdatetime%" livestreamer "hls://http://hls.goodgame.ru/hls/7692.m3u8" best -o "%curdatetime%.ts"
timeout /t 30
taskkill /FI "WINDOWTITLE eq livestreamer-%curdatetime%"
GOTO loop

because code for "fullstamp" https://github.com/chrippa/livestreamer/issues/1244#issuecomment-194840776 gives me an error.

flijloku commented 7 years ago

Via FFMPEG:

@
@echo off
:loop
::Вставляем дату и время в название вода
set h=%TIME:~0,2%
set m=%TIME:~3,2%
set s=%TIME:~6,2%
set curtime=%h%-%m%-%s%
::Заменяем на 0 пробел возникающий если время меньше 10 часов
set curtime=%curtime: =0%
set dd=%DATE:~0,2%
set mm=%DATE:~3,2%
set yyyy=%DATE:~6,4%
set curdate=%yyyy%-%mm%-%dd%
set curdatetime=%curdate%__%curtime%

ffmpeg -i "http://hls.goodgame.ru/hls/7692.m3u8" -ss 00:00:00 -t 00:01:00 -c copy %curdatetime%.mkv
goto loop
pause
dajoka commented 7 years ago

thank you for your time.

I tried the below with "hls" and "hlsvariant":

@echo

:loop
set h=%TIME:~0,2%
set m=%TIME:~3,2%
set s=%TIME:~6,2%
set curtime=%h%-%m%-%s%
set curtime=%curtime: =0%
set dd=%DATE:~0,2%
set mm=%DATE:~3,2%
set yyyy=%DATE:~6,4%
set curdate=%yyyy%-%mm%-%dd%
set curdatetime=%curdate%__%curtime%

start "livestreamer-%curdatetime%" livestreamer "hlsvariant://http://awesome.ru/awesome.m3u8" best -o "%curdatetime%.ts"
timeout /t 120
taskkill /FI "WINDOWTITLE eq livestreamer-%curdatetime%"
GOTO loop

I get: [cli][info] Found matching plugin stream for url hls............

but it doesn't save and files and I get no errors of any other kind...

as far as ffmpeg, I don't even know where to start. I downloaded ffmpeg, created a a bat, placed it in the same dir as ffmpeg.exe, but nothing happens.

flijloku commented 7 years ago

Then I have no idea. What you describe should work. Maybe the links that you use the wrong?

dajoka commented 7 years ago

thank you

flijloku commented 7 years ago

That's what I thought. You are using an extension "mkv" in the bat-file via ffmpeg? It is necessary!