I'm trying to figure out if a video has audio present in it so as to extract the mp3 using ffmpeg. When the video contains no audio channels, ffmpeg creates an empty mp3 file which I'm using to figure out if audio was present in the video in the first place. I'm sure there is a better way to identify if audio is present in a video. Will avprobe help with this? Can anyone point me to a resource or probably a solution?
Edit: Surprisingly, the same command on my server running the latest build of ffprobe doesn't run. It throws an error saying
Unrecognized option 'select_stream'
Failed to set value 'a' for option 'select_stream'
Any ideas how to rectify this out?
By using METADATA_KEY_HAS_AUDIO you can check whether the video has the audio or not.
m4a file extension for MP4 files that only contain audio, while . m4v is sometimes used to indicate that it's a video. But since these are conventions, not rules, the only real way to know is to try opening the file using an MP4-compatible media player or examining the file's contents.
I would use FFprobe (it comes along with FFMPEG):
ffprobe -i INPUT -show_streams -select_streams a -loglevel error
In case there's no audio it ouputs nothing. If there is an audio stream then you get something like:
[STREAM]
index=0
codec_name=mp3
codec_long_name=MP3 (MPEG audio layer 3)
profile=unknown
codec_type=audio
codec_time_base=1/44100
etc
etc...
[/STREAM]
That should be easy enough to parse regardless of the language you're using to make this process automated.
If it is normal video file from the local path, you can do something like this to find whether video has audio file or not.
You need to look into the MediaMetadataRetriever
By using METADATA_KEY_HAS_AUDIO
you can check whether the video has the audio or not.
private boolean isVideoHaveAudioTrack(String path) { boolean audioTrack =false; MediaMetadataRetriever retriever = new MediaMetadataRetriever(); retriever.setDataSource(path); String hasAudioStr = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_AUDIO); if(hasAudioStr.equals("yes")){ audioTrack=true; } else{ audioTrack=false; } return audioTrack; }
Here path is your video file path.
PS: Since it is old question , i am writing this answer to help some other folks , to whom it may help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With