Downloading a Few Seconds of Audio from a Youtube Video with ffmpeg and youtube-dl Results in [youtube]: No such file or directory error - bash

Below is the program that I wrote:
ffmpeg -ss 60 -t 10 -i $(youtube-dl -f 140 https://www.youtube.com/watch?v=kDCk3hLIVXo) output.mp3
This is supposed to get 10 seconds of audio starting at the one minute mark of the video and write it to output.mp3. If I run the youtube-dl command separately, and then the ffmpeg command with the entire video audio as input, it works. But, I do not want to download the entire video as well as create a new file with only a few seconds of audio.
In its current state, I am getting [youtube]: No such file or directory errors. Does anyone know how I can fix this and keep it in one line?

The issue is the output which is returned from youtube-dl is several lines of information, so ffmpeg doesn't know how to deal with it properly.
You'll want to return the actual name of the file without any other information included; a tool like awk or sed can be helpful for this. In addition there will need to be an encoding step added at the end so the audio stream gets copied to the output file (libmp3lame->mp3).
Example:
ffmpeg -ss 60 -t 10 $(youtube-dl -f 140 -g https://www.youtube.com/watch?v=kDCk3hLIVXo | \
sed "s/.*/-i &/") -c:v copy -codec:a libmp3lame output.mp3
This command should return an audio mp3 file 60 seconds in which is 10 seconds in duration.
Result:
output.mp3: Audio file with ID3 version 2.4.0, contains:MPEG ADTS, layer III, v1, 64 kbps, 44.1 kHz, Stereo

Related

How to automatically add date to output file generated in ffmpeg?

I am using Terminal to run ffmpeg commands (Mac OS) in order to record radio shows streamed online. The stream is in m3u8 I want to output it in mp3. So far so good, I am able to achieve that. However, I'd like the output file to read YYYYMMDD-fm93-segal.mp3 where YYYYMMMDD are the date the recording was made.
I am not able to achieve this using -strftime 1 for some reason. When using my code, the output file reads %Y%m&d-fm93-segal.mp3 instead of replacing the strings by the real date.
Here is the line I'm using:
ffmpeg -i "https://cogecomedia.leanstream.co/cogecomedia/CJMFFM.stream/playlist.m3u8" -acodec mp3 -strftime 1 "%Y%m%d-fm93-segal.mp3"
Anyone knows why and could help me with that?
-strftime is not a generic option, but is only supported by some muxers: hls, image, segment.
One method is to use the segment muxer and give it a big -segment_time value if desired:
ffmpeg -i "https://cogecomedia.leanstream.co/cogecomedia/CJMFFM.stream/playlist.m3u8" -f segment -segment_time 24:00:00 -acodec mp3 -strftime 1 "%Y%m%d-fm93-segal.mp3"

avconv save 10 frames and update them in cycle

The way I am saving frames from camera is:
avconv -f video4linux2 -i /dev/video0 -r 5 output_%04d.png
Seems to be working fine... but... I need to save only 10 frames and overwrite the saved first saved all the time... any way to do that? I need to be able to save frames and process them in other application... but process may take longer than record.
Tried to save one frame and overwrite it, but the problem is that file is being used and it simply doesn't work.
There's a roundabout way to do this. You can use the segment muxer. This doesn't support image sequences, though. So, the method is to encode using PNG codec in MOV files and then run a script to extract the PNG stream losslessly to the image format.
ffmpeg -f video4linux2 -i /dev/video0 -r 5 -c:v png
-f segment -segment_time 0.1 -segment_wrap 10 out%d.mov
And then, iterate the command below for all 10 MOVs,
ffmpeg -i out1.mov -c copy out1.png

ffmpeg capture current frame and overwrite the image output file

I am trying to extract the image file from a RTSP stream url every second (could be every 1 min also) and overwrite this image file.
my below code works but it outputs to multiple image jpg files: img1.jpg, img2.jpg, img3.jpg...
ffmpeg -i rtsp://IP_ADDRESS/live.sdp -f image2 -r 1 img%01d.jpg
How to use ffmpeg or perhaps bash scripts in Linux to overwrite the same image file while continuously extract the image at a NOT high frequecy, say 1 min or 10 sec?
To elaborate a bit on the already accepted answer from pragnesh,
FFmpeg
As stated in the ffmpeg documentation:
ffmpeg command line options are specified as
ffmpeg [global_options] {[input_options] -i input_file} ... {[output_options] output_file} ...
So
ffmpeg -i rtsp://<rtsp_source_addr> -f image2 -update 1 img.jpg
Uses output option -f image2 , force output format to image2 format, as part of the muxer stage.
Note that in ffmpeg, if the output file name specifies an image format the image2 muxer will be used by default, so the command could be shortened to:
ffmpeg -i rtsp://<rtsp_source_addr> -update 1 img.jpg
The image2 format muxer expects a filename pattern, such as img%01d.jpg to produce a sequentially numbered series of files. If the update option is set to 1, the filename will be interpreted as just a filename, not a pattern, thereby overwriting the same file.
Using the -r , set frame rate, video option works, but generated me a whole lot of dropping frame messages which was bugging me.
Thanks to another answer on the same topic, I found the fps Video Filter to do a better job.
So my version of the working command is
ffmpeg -i rtsp://<rtsp_source_addr> -vf fps=fps=1/20 -update 1 img.jpg
For some reason still unkown to me the minimum framerate I can achieve from my feed is 1/20 or 0.05.
There also exists the video filter thumbnail, which selects an image from a series of frames but this is more processing intensive and therefore I would not recommend it.
Most of this and more I found on the FFMpeg Online Documentation
AVconv
For those of you who use avconv it is very similar. They are after all forks of what was once a common library. The AVconv image2 documentation is found here.
avconv -i rtsp://<rtsp_source_addr> -vf fps=fps=1/20 -update 1 img.jpg
As Xianlin pointed out there may be a couple other interesting options to use:
-an : Disables audio recording.
Found in Audio Options Section
-r < fps > : sets frame rate
Found in the Video Options Section
used as an output option is actually a a substitute for the fps filter
leading to an alternate version :
avconv -i rtsp://<rtsp_source_addr> -r 1/20 -an -update 1 img.jpg
Hope it helps understand for possible further tweaking ;)
Following command line should work for you.
ffmpeg -i rtsp://IP_ADDRESS/live.sdp -f image2 -updatefirst 1 img.jpg
I couldn't get the option -update working to overwrite the .jpg. Doing some experiments resulted in a working solution (at least for me) with the option -y at the end (upper-case is not working). I also needed http:// instead of rstp:// for this camera.
ffmpeg -i http://xx:yy#192.168.1.xx:yyy/snapshot.cgi /tmp/Capture2.jpg -y
Grab a snapshot from an RTSP video stream every 10 seconds.
#!/bin/bash
#fetch-snapshots.sh
url='rtsp://IP_ADDRESS/live.sdp'
avconv -i $url -r 0.1 -vsync 1 -qscale 1 -f image2 images%09d.jpg
-r rate set frame rate to 0.1 frames a second (this equals to 1 frame every 10 seconds).
Thanks to westonruter, see https://gist.github.com/westonruter/4508842
Furthermore have a look at FFMPEG: Extracting 20 images from a video of variable length
ffmpeg -i rtsp://root:password#192.168.1.1/mpeg4 -ss 00:00:01 -f image2 -vframes 1 thumb.jpg
replace with your rtsp protocol url
make sure 00:00:01
if you put other numbers, the image will be crashed

Add multiple audio files to video at specific points using FFMPEG

I am trying to create a video out of a sequence of images and various audio files using FFmpeg. While it is no problem to create a video containing the sequence of images with the following command:
ffmpeg -f image2 -i image%d.jpg video.mpg
I haven't found a way yet to add audio files at specific points to the generated video.
Is it possible to do something like:
ffmpeg -f image2 -i image%d.jpg -i audio1.mp3 AT 10s -i audio2.mp3 AT 15s video.mpg
Any help is much appreciated!
EDIT:
The solution in my case was to use sox as suggested by blahdiblah in the answer below. You first have to create an empty audio file as a starting point like that:
sox -n -r 44100 -c 2 silence.wav trim 0.0 20.0
This generates a 20 sec empty WAV file. After that you can mix the empty file with other audio files.
sox -m silence.wav "|sox sound1.mp3 -p pad 0" "|sox sound2.mp3 -p pad 2" out.wav
The final audio file has a duration of 20 seconds and plays sound1.mp3 right at the beginning and sound2.mp3 after 2 seconds.
To combine the sequence of images with the audio file we can use FFmpeg.
ffmpeg -i video_%05d.png -i out.wav -r 25 out.mp4
See this question on adding a single audio input with some offset. The -itsoffset bug mentioned there is still open, but see users' comments for some cases in which it does work.
If it works in your case, that would be ideal:
ffmpeg -i in%d.jpg -itsoffset 10 -i audio1.mp3 -itsoffset 15 -i audio2.mp3 out.mpg
If not, you should be able to combine all the audio files with sox, overlaying or inserting silence to produce the correct offsets and then use that as input to FFmpeg. Not as convenient, but guaranteed to work.
One approach I can think of is to create your audio file for the whole duration of the video first and then mux the audio with the video file

Generate a movie with ffmpeg from a changing still image url?

I need to create a movie/stream with ffmpeg from a HTTP url that points to an image. This image gets updated 1 time per second.
I already know how to convert from MPEG-4 to flv for example using the ffmpeg command line, but now I need to start from this still image that gets updated. I would like ffmpeg to 'GET' the url 1 time per second for example.
regards,
Wim
The command line option needed is -loop_input. I am currenly using this command line to do it:
ffmpeg -loop_input -analyzeduration 0 -r 3 -i http://ipaddress/current.jpg -an -re -copyts -f flv output.flv
The -loop_input instructs ffmpeg to read the jpg on the given URL at the input frame rate (3 fps in this example). The -analyzeduration 0 will give a quicker startup and show the first frame of your movie faster. The output can be anything, in the example here, it is a flash movie, but it can be anything ffmpeg supports.

Resources