How to record audio with ffmpeg on linux? - linux

I'd like to record audio from my microphone. My OS is ubuntu. I've tried the following and got errors
$ ffmpeg -f alsa -ac 2 -i hw:1,0 -itsoffset 00:00:00.5 -f video4linux2 -s 320x240 -r 25 /dev/video0 out.mpg
ffmpeg version 0.8.8-4:0.8.8-0ubuntu0.12.04.1, Copyright (c) 2000-2013 the Libav
developers
built on Oct 22 2013 12:31:55 with gcc 4.6.3
*** THIS PROGRAM IS DEPRECATED ***
This program is only provided for compatibility and will be removed in a future release.
Please use avconv instead.
ALSA lib conf.c:3314:(snd_config_hooks_call) Cannot open shared library
libasound_module_conf_pulse.so
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM hw:1,0
[alsa # 0xbda7a0] cannot open audio device hw:1,0 (No such file or directory)
hw:1,0: Input/output error
Then I tried
$ ffmpeg -f oss -i /dev/dsp audio.mp3
ffmpeg version 0.8.8-4:0.8.8-0ubuntu0.12.04.1, Copyright (c) 2000-2013 the Libav
developers
built on Oct 22 2013 12:31:55 with gcc 4.6.3
*** THIS PROGRAM IS DEPRECATED ***
This program is only provided for compatibility and will be removed in a future release.
Please use avconv instead.
[oss # 0x1ba57a0] /dev/dsp: No such file or directory
/dev/dsp: Input/output error
I haven't been able to get ffmpeg to find my microphone. How can I tell ffmpeg to record from my microphone?
It seems the 'Deprecated' message can be ignored because of this topic

I realise this is a bit old. Just in case anyone else is looking:
ffmpeg -f alsa -ac 2 -i default -itsoffset 00:00:00.5 -f video4linux2 -s 320x240 -r 25 -i /dev/video0 out.mpg
This way it will use the default device to record from. You were also missing a -i before the video capture device - /dev/device0
If you want to get more specific you should take a look in /proc/asound.
Check the cards, devices, pcm files and the card subdirectories. You should be able to glean enough information there to be able to make an educated guess; e.g hw:1,0 or hw:2,0
The documentation may provide further clues:
http://www.alsa-project.org/main/index.php/DeviceNames
The same goes for the webcam - it may not be /dev/video0, perhaps you have an external webcam plugged in and its at /dev/video1 - Have a look in the /dev directory and see whats available

solved !
ffmpeg -f pulse -ac 2 -i default -f x11grab -r 30 -s 1920x1080 -i :0.0 -acodec pcm_s16le -vcodec libx264 -preset ultrafast -threads 0 -y /media/t/TBVolume/desktop/output.mkv

First, list your AV devices using:
ffmpeg -list_devices true -f dshow -i dummy
Assuming your audio device is "Microphone Array", you can use:
ffmpeg -f dshow -i audio="Microphone Array" -c:a libmp3lame -b:a 128k OUTPUT.mp3
Here, 128k is the sampling rate. You can check all options for sampling rates (CBR) here.

Related

How to convert video for web with ffmpeg

I am trying to rescale, subclip and convert video for web (html5 video tag). Target browsers : Chrome, Safari, Firefox, Ya Browser.
I am using command like that (changing some params)
ffmpeg -i C.mp4 -ss 00:00:00 -t 10 -vf scale=312x104 -vcodec libx264 -strict -2 -movflags faststart -pix_fmt yuv420p -profile:v high -level 3 -r 25 -an -sn -dn d.mp4 -y
But every time video is not playing in some browser.
I would like to find some way to do that task fast (that's why I am using ffmpeg) and stable (so that any video passed would give me a valid video for all browsers)
I also tried to play with setsar, setdar params, but still no success
Thanks everyone, I guess I found smth suitable for my case
Ffmpeg -i C.mp4 -ss 00:00:00 -t 10 -vf scale=dstw=312:dsth=104:flags=accurate_rnd,setdar=3/1 -vcodec libx264 -level 21 -refs 2 -pix_fmt yuv420p -profile:v high -level 3.1 -color_primaries 1 -color_trc 1 -colorspace 1 -movflags +faststart -r 30 -an -sn -dn d.mp4

How can I clean the sound of aplay received by a caputre card

I am trying to setup my linux desktop to be able to view and listent to the device connected to my capture card. I wrote this 2 liner script to be able to do that however my sound is out of tone and a bit distorted, how could I clean it up?
arecord --buffer-time=1 -f cd - | aplay --buffer-time=1 -c 5 -r 48000 -f S16_LE - 2> /dev/null &
ffplay -f video4linux2 -framerate 30 -video_size 1920x1080 -input_format mjpeg /dev/video1 2> /dev/null &
I also tried to do that with ffmpeg piped to ffplay and the sound is crystal clear however there is 2-3 seconds delay on the video and sound, is there a way to fix this?
ffmpeg -framerate 30 -video_size 1920x1080 -thread_queue_size 1024 -input_format mjpeg -i /dev/video1 -f pulse -i 'Analog Input - USB Video' -r 30 -threads 4 -vcodec libx264 -crf 0 -preset ultrafast -vsync 1 -async 1 -f matroska - |ffplay -
Could you try just using ffplay for your second approach?
ffplay -framerate 30 -video_size 1920x1080 \
-thread_queue_size 1024 -input_format mjpeg -i /dev/video1 \
-f pulse -i 'Analog Input - USB Video'`
I could be off-base as I'm only familiar with ffmpeg and don't personally use ffplay, but they share a lot of things (e.g., backend libraries and command line parsing) so I'm hedging this would work.
Also, what do you mean by "there is 2-3 seconds delay on the video and sound"? Are they 2-3 seconds behind what you are physically seeing and hearing? Or are they out of sync by that many seconds?
[addendum]
Not sure if OP is still checking this post, but there is a solution to combine two inputs for ffplay by using an input filtergraph with movie and amovie filters. The following worked in Windows (despite unacceptably large latency):
ffplay -f lavfi -i \
movie=filename=video="Logitech HD Webcam C310":format_name=dshow:format_opts=rtbufsize=702000k[out0]; \
amovie=filename=audio="Microphone (HD Webcam C310)":format_name=dshow[out1]
Note that this is only for the illustration purpose as dshow device can output multiple streams (though the latency is still too bad for real-time use).
The same should be possible in Linux:
ffplay -f lavfi -i \
movie=filename=/dev/video1:format_name=video4linux2:format_opts='framerate=30:video_size=1920x1080:thread_queue_size=1024:input_format=mjpeg'[out0]; \
amovie=filename='Analog Input - USB Video':format_name=pulse[out1]
(Disclaimer: Untested and it may be missing escaping)
The latency may be better in Linux (and with a higher spec'ed PC than mine) so it might be worth a try.

ffmpeg with Popen (python) on Windows

I am trying to use ffmpeg with Popen. The ffmpeg command I am trying works on cmd but gives me error with Popen.
I am using the standalone ffmpeg .exe:
ffmpeg -f gdigrab -offset_x 10 -offset_y 20 -show_region 1 -i desktop -video_size 1536x864 -b:v 2M -maxrate 1M -bufsize 1M -tune fastdecode -crf 15 -preset ultrafast -pix_fmt yuv420p -r 25 <path>/video.mov -qp 1 -y -an
This gives me Invalid argument, but if I remove the last parameters in order to make the output the last thing on the string, I get a different error:
Output file #0 does not contain any stream
I tried to use -f dshow -i video="UScreenCapture" instead of the gdigrab, but both give me the same error with and without the parameters in the end.
Both commands work on command line.
On command line this ffmpeg -list_devices true -f dshow -i dummy returns this:
[dshow # 000001b24fa6a300] DirectShow video devices (some may be both video and audio devices)
[dshow # 000001b24fa6a300] "Integrated Webcam"
[dshow # 000001b24fa6a300] Alternative name "#device_pnp_\\?\usb#vid_1bcf&pid_2b8a&mi_00#6&2c03619a&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global"
[dshow # 000001b24fa6a300] "UScreenCapture"
[dshow # 000001b24fa6a300] Alternative name "#device_sw_{860BB310-5D01-11D0-BD3B-00A0C911CE86}\UScreenCapture"
[dshow # 000001b24fa6a300] DirectShow audio devices
[dshow # 000001b24fa6a300] "Microphone (Realtek Audio)"
[dshow # 000001b24fa6a300] Alternative name "#device_cm_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\wave_{35EBFC89-7B09-4557-8032-85AA0B688FE9}"
But on popen I can't check it:
-list_devices true -f dshow -i dummy: Invalid argument
For the python part of the code I am using this:
p = subprocess.Popen([getPathForFile("windows/ffmpeg").replace('\\','/'), " -f gdigrab -offset_x 10 -offset_y 20 -show_region 1 -i desktop -video_size 1536x864 -b:v 2M -maxrate 1M -bufsize 1M -tune fastdecode -crf 15 -preset ultrafast -pix_fmt yuv420p -r 25 -qp 1 -y -an "+ path.replace('\\\\','/').replace('\\','/')+"video.mov"], shell=True)
The getPathForFile is a custom function that returns the path. this is correct, mainly because the errors I am getting are from the ffmpeg, so...
I am on a Windows 10. FFmpeg 4.0. Python 3.5.
Any ideas why am I getting these errors on Popen but not on command line and how to fix them? (mainly the second error)
Put each argument in its own string and turn off the shell.
Like this:
import subprocess
import os
cmd = ["-f", "gdigrab", "-offset_x", "10", "-offset_y", "20",
"-show_region", "1", "-video_size", "1536x864", "-i", "desktop",
"-b:v", "2M", "-maxrate", "1M", "-bufsize", "1M", "-tune", "fastdecode",
"-preset", "ultrafast", "-pix_fmt", "yuv420p",
"-r", "25", "-qp", "1", "-y", "-an",
os.path.join(path, "video.mov")]
p = subprocess.Popen([getPathForFile("windows/ffmpeg")]+cmd)
p.communicate()

ffmpeg error: cur_dts is invalid

I'm trying to record video and audio from my webcam with the following command:
ffmpeg -f v4l2 -input_format mjpeg -s uhd2160 -i /dev/video0 -f pulse -channels 1 -i default video.mpg
but it seems to get stuck in some endless loop at the bottom of the output that looks like this:
cur_dts is invalid (this is harmless if it occurs once at the start per stream)
cur_dts is invalid (this is harmless if it occurs once at the start per stream)
cur_dts is invalid (this is harmless if it occurs once at the start per stream)
...
Full output with -report flag enabled: https://pastebin.com/bV1ePJ2u
I can record video and audio separately just fine:
ffmpeg -f v4l2 -input_format mjpeg -s uhd2160 -i /dev/video0 video.mpg
ffmpeg -f pulse -channels 1 -i default audio.wav
So what could be the problem here? I'm on Ubuntu 16.04 kernel 4.14.1
You can combine them into one file by using this command (in cmd for Windows OS)
C:/this is just path of ffmpeg lib/ffmpeg_shared/ffmpeg/bin/ffmpeg -i "video_part.ts" -i "audio_part.aac" -map 0:V:0 -map 1:a:0 -c copy -f mp4 -movflags +faststart "./new/combined_file_name.mp4"

ffmpeg audio and video sync error

./ffmpeg \
-f alsa -async 1 -ac 2 -i hw:2,0 \
-f video4linux2 -vsync 1 -s:v vga -i /dev/video0 \
-acodec aac -b:a 40k \
-r 25 -s:v vga -vcodec libx264 -strict -2 -crf 25 -preset fast -b:v 320K -pass 1 \
-f flv rtmp://192.168.2.105/live/testing
with the above command i able to stream with fps of 25 but their is no audio and video synchronization i.e., audio is faster than video,i am using ffmpeg 0.11.1 version on the pandaboard for an rtmp streaming ,help me out to solve this problem.
Thanks
Ameeth
Don't use -pass 1 if you're not actually doing two-pass encoding.
From the docs (emphasis added):
‘-pass[:stream_specifier] n (output,per-stream)’
Select the pass number (1 or 2). It is used to do two-pass video encoding. The statistics of the video are recorded in the first pass into a log file (see also the option -passlogfile), and in the second pass that log file is used to generate the video at the exact requested bitrate. On pass 1, you may just deactivate audio and set output to null, examples for Windows and Unix:
ffmpeg -i foo.mov -c:v libxvid -pass 1 -an -f rawvideo -y NUL
ffmpeg -i foo.mov -c:v libxvid -pass 1 -an -f rawvideo -y /dev/null
I was streaming to Twitch, and, funnily enough, removing the -r option made video sync with the audio. Now, you might want to have the framerate limited in some way; unfortunately, I have no solution for that, but it does allow to sync the video with the audio very well.

Resources