LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-11-2021, 09:49 PM   #1
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
how to cleanly stop ffmpeg?


i am wanting to download a live video feed. i have experimented with ffmpeg and it seems to do the job. but it keeps on doing the job. the live feed continues for a very long time. if i just let it keep running for hours and hours, the feed stops and ffmpeg comes to a graceful stop and i get a .mp4 file that works. but if i press ^C, it kills ffmpeg which does not finish the file it was writing.

during the download it is writing into a file that has a name like the URL but ends in .mp4.part which grows and grows. when the feed ends, ffmpeg makes it into a .mp4 file. but, if it is killed, the file never gets finished.

the ffmpeg man page says nothing about signals. i'd like to know how to gracefully stop ffmpeg and have it finish as if the feed has ended. i want to play the video up to that point long before the feed ends.

also, how can i do the finish process for the leftover files after ffmpeg is killed? i have a few of these i'd like to save. mplayer won't play the unfinished ones.
 
Old 01-12-2021, 01:35 AM   #2
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
In a terminal? Enter 'q'.

BTW, ffmpeg shutting down gracefully is not necessarily the same as producing a playable file.

Last edited by ondoho; 01-12-2021 at 01:36 AM.
 
Old 01-12-2021, 02:43 PM   #3
jefro
Moderator
 
Registered: Mar 2008
Posts: 21,980

Rep: Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624
https://askubuntu.com/questions/4369...period-of-time
 
Old 01-12-2021, 03:09 PM   #4
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
You want to record something for a limited time?
Code:
ffmpeg -t 00:30:00 -c:a copy -c:v copy Ouput.ts
Don't encode on the fly. Dump it to a transport stream,(ffmpeg knows what that is), then re-encode it later if you want to.

You want to start/stop on clock times? Such as a TV program?

Code:
#Set wake date/time here, sleep
rtcwake -m mem --date "2020-08-10 20:56:00"

#Allow everything to wake up
sleep 10

#Record
ffmpeg -i "$url" -c:a copy -c:v copy out.ts

#Wait
sleep 1800

#Sledge hammer
pkill ffmpeg

#Return to sleep
systemctl suspend

Last edited by teckk; 01-12-2021 at 03:11 PM.
 
1 members found this post helpful.
Old 01-12-2021, 05:25 PM   #5
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
Quote:
Originally Posted by ondoho View Post
BTW, ffmpeg shutting down gracefully is not necessarily the same as producing a playable file.
but having it cease to continue running seems to not produce a playable file. the man page appears to say nothing about this unless it is using some strange terminology.
 
Old 01-12-2021, 05:30 PM   #6
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
Quote:
Originally Posted by jefro View Post
in my case i don't know how long i want it to run. i only know i want to stop it when that time comes around. so giving a time in advance is not an option.
 
Old 01-12-2021, 05:38 PM   #7
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
Quote:
Originally Posted by teckk View Post
You want to record something for a limited time?
Code:
ffmpeg -t 00:30:00 -c:a copy -c:v copy Ouput.ts
Don't encode on the fly. Dump it to a transport stream,(ffmpeg knows what that is), then re-encode it later if you want to.

You want to start/stop on clock times? Such as a TV program?
i want to start recording a live feed without any idea when it needs to stop. at some point i will know that i need to stop. then i would need to tell it so. typing q does not work. that may be because i ran youtube-dl which runs ffmpeg. i am not running ffmpeg myself.
 
Old 01-12-2021, 05:49 PM   #8
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
Quote:
Originally Posted by ondoho View Post
In a terminal? Enter 'q'.
i am running youtube-dl as a command. but i can see that it is running ffmpeg. i don't know how long i want it to run, in advance.

as the run progresses there is a .mp4.part file which cannot be played. if the feed ends normally, then it has a .mp4 file which plays OK, but for hours longer because i could not end the feed.

if there is a way to have ffmpeg "fix" the .mp4.part file and make the .mp4 file, then that can be done after killing youtube-dl/ffmpeg.
 
Old 01-12-2021, 07:21 PM   #9
jefro
Moderator
 
Registered: Mar 2008
Posts: 21,980

Rep: Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624Reputation: 3624
What are you trying to record? Some live event on youtube like a ship passing by?

You'd have to guess the time frame I'd think if the video never stops.
 
Old 01-12-2021, 08:49 PM   #10
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
Quote:
Originally Posted by jefro View Post
What are you trying to record? Some live event on youtube like a ship passing by?

You'd have to guess the time frame I'd think if the video never stops.
it's kinda like that. the video does eventually stop, but only after hours. it typically runs for 48 to 60 hours. this spring it will run longer, like 120 to 168 hours.

i did figure out how to finish those .mp4.part files and get a working .mp4 file:

Code:
ffmpeg -i foo.mp4.part -c copy -f mp4 foo.mp4
the man page didn't literally say how or even mention "part" but i tried mixing and matching stuff and hit what works.

edit:

i will try the transport stream thing next. if that can eliminate skipping segments then it's time to tweak some extractors in youtube-dl to invoke ffmpeg that way.

Last edited by Skaperen; 01-12-2021 at 08:55 PM.
 
Old 01-13-2021, 12:36 AM   #11
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
It's just another case of "why read man pages when I can ask others to do it for me".
Both youtube-dl and ffmpeg have extensive web pages (in addition to plenty online material).

Last edited by ondoho; 01-13-2021 at 12:00 PM.
 
Old 01-13-2021, 06:11 AM   #12
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
Live stream (picked first one I found)
Code:
youtube-dl -F https://m.youtube.com/watch?v=bELfdiCGQhU
[youtube] bELfdiCGQhU: Downloading webpage
[youtube] bELfdiCGQhU: Downloading m3u8 information
[youtube] bELfdiCGQhU: Downloading MPD manifest
[info] Available formats for bELfdiCGQhU:
format code  extension  resolution note
91           mp4        256x144    HLS  197k , avc1.42c00b, 30.0fps, mp4a.40.5@ 48k
92           mp4        426x240    HLS  338k , avc1.4d4015, 30.0fps, mp4a.40.5@ 48k
93           mp4        640x360    HLS  829k , avc1.4d401e, 30.0fps, mp4a.40.2@128k
94           mp4        854x480    HLS 1380k , avc1.4d401f, 30.0fps, mp4a.40.2@128k
95           mp4        1280x720   HLS 2593k , avc1.4d401f, 30.0fps, mp4a.40.2@256k
96           mp4        1920x1080  HLS 4715k , avc1.640028, 30.0fps, mp4a.40.2@256k (best)
Code:
a=$(youtube-dl -f 92 -g https://m.youtube.com/watch?v=bELfdiCGQhU)
ffmpeg -i "$a" -c:a copy -c:v copy Out.ts
...
hunk_host,initcwndbps,mh,mm,mn,ms,mv,mvi,pl/lsig/AG3C_xAwRQIgTSSKCO1DMKvl2gW8qdi4_5y\
UFE0DFxXK-m5ig7Cc9ccCIQDHV9aZczz8Z5hE24_Emc7hBabExYK0pRgtzcaYxC9iDg%3D%3D/playlist/\
index.m3u8/sq/15914/goap/clen%3D31810%3Blmt%3D1610539590622733/govp/clen%3D35731%3Blmt\
%3D1610539590622733/dur/5.005/file/seg.ts' for reading
frame=  748 fps= 49 q=-1.0 Lsize=     562kB time=00:00:24.99 bitrate= 184.3kbits/s speed=1.62x
Stopped ffmpeg with Ctrl+c
Out.ts is playable.

Code:
file Out.ts
Out.ts: data

ffprobe Out.ts
...
Input #0, mpegts, from 'Out.ts':
  Duration: 00:00:24.94, start: 1.400000, bitrate: 184 kb/s
  Program 1 
    Metadata:
      service_name    : Service01
      service_provider: FFmpeg
    Stream #0:0[0x100]: Video: h264 (Main) ([27][0][0][0] / 0x001B), yuv420p(tv, bt709, progressive), 426x240 [SAR 1:1 DAR 71:40], 30 fps, 240 tbr, 90k tbn, 60 tbc
    Stream #0:1[0x101]: Audio: aac (HE-AAC) ([15][0][0][0] / 0x000F), 44100 Hz, stereo, fltp, 49 kb/s
    
ffmpeg -i Out.ts -c:a copy -c:v copy Out.mp4

file Out.mp4
Out.mp4: ISO Media, MP4 Base Media v1 [IS0 14496-12:2003]
 
Old 01-13-2021, 01:47 PM   #13
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
how many frames were captured? i seem to be getting some method called HLS that is resulting in multiple web requests that sometimes reports skipping segments due to expiring from playlists. can you do that test after capturing 5 minutes and being sure you get the whole 5 minutes with no freezing parts. can that file be played while the capture is running?

i don't follow the way you did that command. what is $a?

Last edited by Skaperen; 01-13-2021 at 01:50 PM.
 
Old 01-13-2021, 03:45 PM   #14
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
Quote:
what is $a?
A variable.
https://mywiki.wooledge.org/BashGuide

Quote:
i seem to be getting some method called HLS that is resulting in multiple web requests
https://en.wikipedia.org/wiki/HTTP_Live_Streaming
 
Old 01-13-2021, 04:40 PM   #15
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
You need to learn to use your tools, and some bash, and read what a hls segmented stream is.

Looking for a live stream, something normal(Not politics)...Ah yes NASA.
Code:
youtube-dl -F https://m.youtube.com/watch?v=DDU-rZs-Ic4
[youtube] DDU-rZs-Ic4: Downloading webpage
[youtube] DDU-rZs-Ic4: Downloading m3u8 information
[youtube] DDU-rZs-Ic4: Downloading MPD manifest
[info] Available formats for DDU-rZs-Ic4:
format code  extension  resolution note
91           mp4        256x144    HLS  197k , avc1.42c00b, 30.0fps, mp4a.40.5@ 48k
92           mp4        426x240    HLS  338k , avc1.4d4015, 30.0fps, mp4a.40.5@ 48k
93           mp4        640x360    HLS  829k , avc1.4d401e, 30.0fps, mp4a.40.2@128k
94           mp4        854x480    HLS 1380k , avc1.4d401f, 30.0fps, mp4a.40.2@128k
95           mp4        1280x720   HLS 2593k , avc1.4d401f, 30.0fps, mp4a.40.2@256k
96           mp4        1920x1080  HLS 4715k , avc1.640028, 30.0fps, mp4a.40.2@256k (best)
Code:
b=$(youtube-dl -f 93 -g https://m.youtube.com/watch?v=DDU-rZs-Ic4)

echo "$b"
https://manifest.googlevideo.com/api/manifest/hls_playlist/expire/1610596795/.../playlist/index.m3u8

curl -LI "$b"
HTTP/2 200 
content-type: application/vnd.apple.mpegurl
date: Wed, 13 Jan 2021 22:03:12 GMT
pragma: no-cache
expires: Fri, 01 Jan 1990 00:00:00 GMT
cache-control: no-cache, must-revalidate
server: HTTP server (unknown)
content-length: 6384
...

ffplay "$b"
...
[hls @ 0x7f269c000bc0] Skip ('#EXT-X-VERSION:3')B sq=    0B f=0/0   
[hls @ 0x7f269c000bc0] Skip ('#EXT-X-DISCONTINUITY-SEQUENCE:716')
[hls @ 0x7f269c000bc0] Skip ('#EXT-X-PROGRAM-DATE-TIME:2021-01-13T22:03:46.591+00:00'
...
Opening 'https://r5---sn-vgqs7ney.googlevideo.com/v.../4.999/file/seg.ts' for reading
...
  Duration: N/A, start: 639.171689, bitrate: N/A
  Program 0 
    Metadata:
      variant_bitrate : 0
    Stream #0:0: Audio: aac (LC) ([15][0][0][0] / 0x000F), 48000 Hz, stereo, fltp
    Metadata:
      variant_bitrate : 0
    Stream #0:1: Video: h264 (Main) ([27][0][0][0] / 0x001B), yuv420p(tv, bt709), 640x360 [SAR 1:1 DAR 16:9], 30 fps, 30 tbr, 90k tbn, 60 tbc
    Metadata:
      variant_bitrate :
If you look into that index.m3u8, you'll find the segmented playlist
Code:
curl "$b" -o test.m3u8
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  6384  100  6384    0     0  17347      0 --:--:-- --:--:-- --:--:-- 17347

cat test.m3u8

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:5
#EXT-X-MEDIA-SEQUENCE:3990038
#EXT-X-DISCONTINUITY-SEQUENCE:716
#EXT-X-PROGRAM-DATE-TIME:2021-01-13T22:18:01.592+00:00
#EXTINF:4.999,
https://r5---sn-vgqs7ney.googlevideo.com/v....
And you should see a pattern to the end of each segment url.
%3D1610465933234115/dur/4.999/file/seg.ts
%3D1610465933234125/dur/5.001/file/seg.ts
%3D1610465933234135/dur/4.999/file/seg.ts

I could also play that with
Code:
ffplay $(youtube-dl -f 93 -g https://m.youtube.com/watch?v=DDU-rZs-Ic4)
I could get the playlist with
Code:
mpv $(youtube-dl -f 93 -g https://m.youtube.com/watch?v=DDU-rZs-Ic4) --stream-dump=myfile.m3u8
See:
man youtube-dl
man ffmpeg
man ffplay
man curl
man mpv
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to stop or shutdown wvdial cleanly? shogun1234 Linux - Wireless Networking 3 12-09-2010 07:14 AM
Help me in installing ffmpeg, ffmpeg-PHP, Mplayer, Mencoder, flv2tool, LAME MP3 Encod mitesh.ever Red Hat 5 05-16-2009 12:14 PM
Does the latest version of ffmpeg not work with ffmpeg-php? whitey4900 Linux - Software 0 08-04-2008 05:16 PM
How do i cleanly stop a service? n0vembr Linux - General 18 07-29-2005 04:11 AM
Stop java program(threaded program..should end cleanly) rmanocha Programming 4 11-09-2004 09:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration