How to keep the last 1 min video streaming using ffmpeg? - ffmpeg

I have a UVC camera /dev/video1. The Camera will be always on. but I only care about the last 1 min data stream.
after searching online I got a ffmpeg cmd:
./ffmpeg -f v4l2 -input_format mjpeg -video_size 320x240 -i /dev/video1 -c copy -f segment -segment_time 60 -segment_wrap 2 output.mkv
However I got a error and here is the result
libavutil 56. 56.100 / 56. 56.100
libavcodec 58. 97.100 / 58. 97.100
libavformat 58. 49.100 / 58. 49.100
libavdevice 58. 11.101 / 58. 11.101
libavfilter 7. 87.100 / 7. 87.100
libswscale 5. 8.100 / 5. 8.100
libswresample 3. 8.100 / 3. 8.100
libpostproc 55. 8.100 / 55. 8.100
Input #0, matroska,webm, from '/sdcard/Movies/output.mkv':
Metadata:
ENCODER : Lavf58.49.100
Duration: N/A, start: 0.000000, bitrate: N/A
Stream #0:0: Video: mjpeg (Baseline), yuvj422p(pc, bt470bg/unknown/unknown), 320x240, 30 fps, 30 tbr, 1k tbn, 1k tbc (default)
[matroska # 0x3899e10] Invalid segment filename template 'output.mkv'
Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument

You're using the segment muxer, which expects to write multiple files. So it expects the filename to have a format specifier for the serial number. The simplest one is %d which will be replaced by a number without padding.
ffmpeg -f v4l2 -input_format mjpeg -video_size 320x240 -i /dev/video1 -c copy -f segment -segment_time 60 -segment_wrap 2 output%d.mkv

Related

FFMPEG and MP3: equalizer filter has no effect at all

Practicing FFMPEG filters on MP3s was great until I got stuck here with no luck reading from FFMPEG docs or around the web: equalizer filter has no effect at all on my MP3.
First I probed the file:
libavutil 56. 49.100 / 56. 49.100
libavcodec 58. 89.100 / 58. 89.100
libavformat 58. 43.100 / 58. 43.100
libavdevice 58. 9.103 / 58. 9.103
libavfilter 7. 83.100 / 7. 83.100
libswscale 5. 6.101 / 5. 6.101
libswresample 3. 6.100 / 3. 6.100
libpostproc 55. 6.100 / 55. 6.100
Input #0, mp3, from '.\cierre.mp3':
Metadata:
major_brand : 3gp4
minor_version : 0
compatible_brands: isom3gp4
com.android.version: 10
encoder : Lavf58.43.100
Duration: 00:00:14.92, start: 0.000000, bitrate: 64 kb/s
Stream #0:0: Audio: mp3, 44100 Hz, mono, fltp, 64 kb/s
Then I triy different combinations, understanding this would increase frequency at 1KHZ by 20 DBs, but I hear no difference inbetween source and result:
.\ffmpeg -i .\cierre.mp3 -filter_complex equalizer=f=1000:g=20:c=1 -c:a libmp3lame equalized.mp3
Also if I ommit the (only?) channel (because as per FFPROBE it is "MONO")
.\ffmpeg -i .\cierre.mp3 -filter_complex equalizer=f=1000:g=20:c=1 libmp3lame equalized.mp3
Is there anything to consider about the MP3? If I use VLC player's equalizer "1khz" is one of the frequencies. Other frequencies like 60HZ and 6000HZ do not show any impact either.
Thanks!
Ahah sorry, I was just no making significant changes, it was working just fine all the time, i-e- +20dB to frequency 60, too low, not noticeable - ofcourse it seems to deppend on the source properties itself.
Thanks!

How to record aac audio from url?

I record audio successfully from an URL that it seems to be mp3 source, sending this command.
$ ffmpeg -y -t "00:01:00" -i $url1 -c copy url1.mp3
ffmpeg version N-93762-ge384f6f2f9 Copyright (c) 2000-2019 the FFmpeg developers
built with gcc 7.4.0 (GCC)
configuration:
libavutil 56. 26.100 / 56. 26.100
libavcodec 58. 52.100 / 58. 52.100
libavformat 58. 27.103 / 58. 27.103
libavdevice 58. 7.100 / 58. 7.100
libavfilter 7. 50.100 / 7. 50.100
libswscale 5. 4.100 / 5. 4.100
libswresample 3. 4.100 / 3. 4.100
Input #0, mp3, from 'http://someurl1:1234':
Now, I get error when I try to record from another URL that seems to be AAC audio source.
$ ffmpeg -y -t "00:01:00" -i $url2 -c copy url2.mp3
ffmpeg version N-93762-ge384f6f2f9 Copyright (c) 2000-2019 the FFmpeg developers
built with gcc 7.4.0 (GCC)
configuration:
libavutil 56. 26.100 / 56. 26.100
libavcodec 58. 52.100 / 58. 52.100
libavformat 58. 27.103 / 58. 27.103
libavdevice 58. 7.100 / 58. 7.100
libavfilter 7. 50.100 / 7. 50.100
libswscale 5. 4.100 / 5. 4.100
libswresample 3. 4.100 / 3. 4.100
Input #0, aac, from 'http://someurl2:1234':
Metadata:
icy-notice1 : <BR>This stream requires Winamp<BR>
icy-notice2 : SHOUTcast DNAS/posix(linux x64) v2.5.5.733<BR>
icy-name : some name
icy-genre : Talk
icy-br : 48
icy-sr : 22050
icy-url :
icy-pub : 0
StreamTitle : some title
Duration: N/A, bitrate: 47 kb/s
Stream #0:0: Audio: aac (HE-AACv2), 44100 Hz, stereo, fltp, 47 kb/s
[mp3 # 0x80003c280] Invalid audio stream. Exactly one MP3 audio stream is required.
Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Last message repeated 1 times
If I try to save it as aac file I get this message:
$ ffmpeg -y -t "00:01:00" -i $url2 -c copy url2.aac
ffmpeg version N-93762-ge384f6f2f9 Copyright (c) 2000-2019 the FFmpeg developers
built with gcc 7.4.0 (GCC)
configuration:
libavutil 56. 26.100 / 56. 26.100
libavcodec 58. 52.100 / 58. 52.100
libavformat 58. 27.103 / 58. 27.103
libavdevice 58. 7.100 / 58. 7.100
libavfilter 7. 50.100 / 7. 50.100
libswscale 5. 4.100 / 5. 4.100
libswresample 3. 4.100 / 3. 4.100
Input #0, aac, from 'http://someurl2:1234':
Metadata:
icy-notice1 : <BR>This stream requires Winamp<BR>
icy-notice2 : SHOUTcast DNAS/posix(linux x64) v2.5.5.733<BR>
icy-name : some name
icy-genre : Talk
icy-br : 48
icy-sr : 22050
icy-url :
icy-pub : 0
StreamTitle : some title
Duration: N/A, bitrate: 48 kb/s
Stream #0:0: Audio: aac (HE-AACv2), 44100 Hz, stereo, fltp, 48 kb/s
Output #0, adts, to 'url2.aac':
Metadata:
icy-notice1 : <BR>This stream requires Winamp<BR>
icy-notice2 : SHOUTcast DNAS/posix(linux x64) v2.5.5.733<BR>
icy-name : some name
icy-genre : Talk
icy-br : 48
icy-sr : 22050
icy-url :
icy-pub : 0
StreamTitle : some title
encoder : Lavf58.27.103
Stream #0:0: Audio: aac (HE-AACv2), 44100 Hz, stereo, fltp, 48 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
size= 0kB time=00:00:00.00 bitrate=N/A speed= 0x
video:0kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
Output file is empty, nothing was encoded (check -ss / -t / -frames parameters if used)
How to record when the source audio from URL is aac?
Is there a way to identify before recording if it is mp3 or aac? Thanks in advance
In your first command, using -c copy is wrong, because you need to reencode from aac (HE-AACv2) to mp3.
See ffmpeg documentation:
a special value copy (output only) to indicate that the stream is not to be re-encoded
I suggest you try this:
ffmpeg -y -t "00:01:00" -i [stream URL] -codec:a libmp3lame output.mp3
Unfortunately, I could not test it against the URL you provided in the comments (http://dreamsiteradiocp4.com:8120/: Connection refused), but it successfully worked with AAC streams listed at fmstream.org.
Reference: video.stackexchange.com

Trying to resize m3u8 videos with ffmpeg

I have a m3u8 vod video on my server. It's working correctly. Now I want to create a smaller size for mobile devices.
Need to create a 480p m3u8 file.
I've tried this code but it's not worked.
ffmpeg -i HD.m3u8 -vf scale=842:-1 -c:a copy -start_number 0 -hls_time 8 -hls_list_size 0 -f hls ./SM.m3u8
And here is my error code:
libavutil 56. 22.100 / 56. 22.100
libavcodec 58. 35.100 / 58. 35.100
libavformat 58. 20.100 / 58. 20.100
libavdevice 58. 5.100 / 58. 5.100
libavfilter 7. 40.101 / 7. 40.101
libavresample 4. 0. 0 / 4. 0. 0
libswscale 5. 3.100 / 5. 3.100
libswresample 3. 3.100 / 3. 3.100
libpostproc 55. 3.100 / 55. 3.100
[hls,applehttp # 0x55a16f22a880] Opening 'HD0.ts' for reading
Input #0, hls,applehttp, from 'HD.m3u8':
Duration: 02:05:08.29, start: 1.460111, bitrate: 0 kb/s
Program 0
Metadata:
variant_bitrate : 0
Stream #0:0: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p, 1920x800 [SAR 1:1 DAR 12:5], 24 fps, 24 tbr, 90k tbn, 48 tbc
Metadata:
variant_bitrate : 0
Stream #0:1: Audio: aac (LC) ([15][0][0][0] / 0x000F), 44100 Hz, stereo, fltp
Metadata:
variant_bitrate : 0
Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
[libx264 # 0x55a16f267880] height not divisible by 2 (842x351)
Error initializing output stream 0:0 -- Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height
Conversion failed!
Is it possible to convert it from a m3u8 file or do I need the original mp4 file for this?
H.264 4:2:0 sampled video has to have even dimensions, so it should be -vf scale=842:-2.
Also, for -hls_time 8, you have to ensure keyframes exist every 8 seconds. Add -force_key_frames expr:gte(t,n_forced*8)

ffmpeg issue with output from video stream

I have two video streams from which I'd like to take one frame of each, both of them are RTSP. I'm using the same FFMPEG instruction for both of them but changing the URL of the stream, the first one works but the second one throws the error:
method SETUP failed: 455 Method Not Valid In This State
Can anyone tell me what could be the reason for this error and how to solve it?
WORKING
ffmpeg -ss 1 -i rtsp://streamreader:trudat55#69.84.126.216:88/videoMain -an -vcodec mjpeg -vframes 1 -aspect 16:9 -q:v 2 -y test.jpg
ffmpeg version 2.8.2 Copyright (c) 2000-2015 the FFmpeg developers
built with gcc 4.9.2 (Debian 4.9.2-10)
configuration: --enable-gpl --enable-avresample --enable-libopencore-amrnb --enable-libx264 --enable-libxvid --enable-postproc --enable-version3 --enable-shared --enable-pic --extra-ldexeflags=-pie
libavutil 54. 31.100 / 54. 31.100
libavcodec 56. 60.100 / 56. 60.100
libavformat 56. 40.101 / 56. 40.101
libavdevice 56. 4.100 / 56. 4.100
libavfilter 5. 40.101 / 5. 40.101
libavresample 2. 1. 0 / 2. 1. 0
libswscale 3. 1.101 / 3. 1.101
libswresample 1. 2.101 / 1. 2.101
libpostproc 53. 3.100 / 53. 3.100
[rtsp # 0x55a60a0a7420] UDP timeout, retrying with TCP
Guessed Channel Layout for Input Stream #0.1 : mono
Input #0, rtsp, from 'rtsp://streamreader:password#69.84.126.216:88/videoMain':
Metadata:
title : IP Camera Video
comment : videoMain
Duration: N/A, start: 0.200044, bitrate: N/A
Stream #0:0: Video: h264 (Baseline), yuv420p, 640x480, 50 tbr, 90k tbn, 180k tbc
Stream #0:1: Audio: pcm_mulaw, 8000 Hz, 1 channels, s16, 64 kb/s
[swscaler # 0x55a60a102460] deprecated pixel format used, make sure you did set range correctly
Output #0, image2, to 'test.jpg':
Metadata:
title : IP Camera Video
comment : videoMain
encoder : Lavf56.40.101
Stream #0:0: Video: mjpeg, yuvj420p(pc), 640x480 [SAR 4:3 DAR 16:9], q=2-31, 200 kb/s, 50 fps, 50 tbn, 50 tbc
Metadata:
encoder : Lavc56.60.100 mjpeg
Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> mjpeg (native))
Press [q] to stop, [?] for help
frame= 1 fps=0.5 q=2.0 Lsize=N/A time=00:00:00.02 bitrate=N/A dup=1 drop=1
video:66kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
NOT WORKING
ffmpeg -ss 1 -i rtsp://camaras.corredorautomotriz.cl:554/live.sdp -an -vcodec mjpeg -vframes 1 -aspect 16:9 -q:v 2 -y test.jpg
ffmpeg version 2.8.2 Copyright (c) 2000-2015 the FFmpeg developers
built with gcc 4.9.2 (Debian 4.9.2-10)
configuration: --enable-gpl --enable-avresample --enable-libopencore-amrnb --enable-libx264 --enable-libxvid --enable-postproc --enable-version3 --enable-shared --enable-pic --extra-ldexeflags=-pie
libavutil 54. 31.100 / 54. 31.100
libavcodec 56. 60.100 / 56. 60.100
libavformat 56. 40.101 / 56. 40.101
libavdevice 56. 4.100 / 56. 4.100
libavfilter 5. 40.101 / 5. 40.101
libavresample 2. 1. 0 / 2. 1. 0
libswscale 3. 1.101 / 3. 1.101
libswresample 1. 2.101 / 1. 2.101
libpostproc 53. 3.100 / 53. 3.100
[rtsp # 0x55b98f2de420] UDP timeout, retrying with TCP
[rtsp # 0x55b98f2de420] method SETUP failed: 455 Method Not Valid In This State
[rtsp # 0x55b98f2de420] Could not find codec parameters for stream 0 (Video: mjpeg, none(bt470bg/unknown/unknown)): unspecified size
Consider increasing the value for the 'analyzeduration' and 'probesize' options
Input #0, rtsp, from 'rtsp://camaras.corredorautomotriz.cl:554/live.sdp':
Metadata:
title : RTSP server
Duration: N/A, start: 0.000000, bitrate: N/A
Stream #0:0: Video: mjpeg, none(bt470bg/unknown/unknown), 90k tbr, 90k tbn, 90k tbc
Stream #0:1: Data: none
Output #0, image2, to 'test.jpg':
Output file #0 does not contain any stream
I found that if I specify that the protocol is TCP with the instruction -rtsp_transport tcp then i don't get an error:
ffmpeg -ss 5 -rtsp_transport tcp -i rtsp://camaras.corredorautomotriz.cl:554/live.sdp -s 640x480 -aspect 16:9 -b:v 800k -r 24 video.flv
But I would still like to know the reason and solution of the problem I got before, because I am not sure I can use this parameter. Anyone knows?
Thanks
As I see in your output, ffmpeg failed to determine input stream type in the second case (rtsp://camaras.corredorautomotriz.cl:554/live.sdp).
I checked it, and it got me mpeg4:
Input #0, rtsp, from 'rtsp://camaras.corredorautomotriz.cl:554/live.sdp':
Metadata:
title : RTSP server
Duration: N/A, start: 0.000000, bitrate: N/A
Stream #0:0: Video: mpeg4 (Simple Profile), yuv420p, 720x480 [SAR 1:1 DAR 3:2], 30 tbr, 30k tbn
Thus possibly you compiled ffmpeg without mpeg4 decoder (or with broken decoder).

ffmpeg & png watermark issue

I tried to create a watermark (using a png image) on a video like this:
ffmpeg -i test.wmv -b:a 300k -ar 22050 -t 10 -f flv -s 352x288 -vf "movie = watermark_logo352.png [watermark]; [in][watermark] overlay =0:0 [out]" out.flv
but I get the error:
ffmpeg version 0.10.4 Copyright (c) 2000-2012 the FFmpeg developers
built on Jun 14 2012 13:14:31 with gcc 4.4.5 configuration:
--prefix=/home/username --enable-cross-compile --enable-shared --arch=amd64 --target-os=linux --disable-yasm --enable-decoder=png --enable-encoder=png
libavutil 51. 35.100 / 51. 35.100
libavcodec 53. 61.100 / 53. 61.100
libavformat 53. 32.100 / 53. 32.100
libavdevice 53. 4.100 / 53. 4.100
libavfilter 2. 61.100 / 2. 61.100
libswscale 2. 1.100 / 2. 1.100
libswresample 0. 6.100 / 0. 6.100
Input #0, asf, from 'test.wmv':
Metadata:
> WMFSDKVersion : 9.00.00.2980
> WMFSDKNeeded : 0.0.0.0000
> IsVBR : 1
> VBR Peak : 351
> Buffer Average : 728 Duration: 00:00:05.59, start: 0.000000, bitrate: 574 kb/s
> Stream #0:0(jpn): Audio: wmav2 (a[1][0][0] / 0x0161), 22050 Hz, 2 channels, s16, 32 kb/s
> Stream #0:1(jpn): Video: wmv1 (WMV1 / 0x31564D57), yuv420p, 352x288, 520 kb/s, SAR 8:9 DAR 88:81, 29.97 tbr, 1k tbn, 1k tbc File
> 'out2.flv' already exists. Overwrite ? [y/N] y w:352 h:288
> pixfmt:yuv420p tb:1/1000000 sar:8/9 sws_param:
[image2 # 0x551f880] decoding for stream 0 failed
[image2 # 0x551f880] Could not find codec parameters (Video: png)
[movie # 0x551f440] Failed to find stream info
[movie # 0x551f440] Failed to find any codec
Error initializing filter 'movie' with args 'watermark_logo352.png'
Error opening filters!
When I use a jpg, it works like a charm.
I'm use ffmpeg v 0.10.4 on Debian 6 Squeeze.
Any help would be much appreciated.
EDIT
The problem is simpler than i thought. If i use ffmpeg -i with any png image i get a similar error:
libavutil 51. 35.100 / 51. 35.100
libavcodec 53. 61.100 / 53. 61.100
libavformat 53. 32.100 / 53. 32.100
libavdevice 53. 4.100 / 53. 4.100
libavfilter 2. 61.100 / 2. 61.100
libswscale 2. 1.100 / 2. 1.100
libswresample 0. 6.100 / 0. 6.100
libpostproc 52. 0.100 / 52. 0.100
[image2 # 0xc8b73a0] decoding for stream 0 failed
[image2 # 0xc8b73a0] Could not find codec parameters (Video: png)
watermark.png: could not find codec parameters
It appears your compiled without zlib support which is a requirement for PNG decoding and encoding (refer to the code of the FFmpeg configure file to see what else requires it).
For Debian/Ubuntu this means you need zlib1g-dev, or for CentOS zlib-devel, as a build dependency and re-compile FFmpeg. It is automatically detected by FFmpeg, so you won't need to add additional ./configure parameters meaning you can also omit --enable-decoder=png --enable-encoder=png.
See the various FFmpeg compile guides at the FFmpeg Wiki, or simply download a build of ffmpeg.
replace [watermark] with [wm] and it works like a charm.
I use this:
-vf "movie=0:png:./watermark.png [wm];[in][wm] overlay=main_w-overlay_w-10:main_h-overlay_h-10 [out]"
(for right bottom watermark)

Resources