amazon-s3audiovideoffmpegaws-media-convert

How to tell from a media file if it's a video or an audio?


I have some files - audio and video files - stored in an aws s3 bucket, which can have different containers and stuff, and I would like to send them to the aws mediaConvert(MC). My issue is that I need to know whenever the file is an audio or a video file, because I need to configure the MC accordingly. The main app on the server is php based, but this information might not be that interesting.

I have ffmpeg, and I already use ffprobe to get the file duration, however I can't tell from the returning data if it's a video or audio-only file. And in an another question someone said that maybe I can get this info by using the ffmpeg, but as far as I know that requires me to download the file from the s3 to the server. There is also an s3 function that I can use to get the header of a specific object, which contains the mime type of the file, however this sometimes returns as audio/mpeg for video files, which then configures the MC incorrectly.

I could also check the error message from the first job create of MC, which then tells me about the issue, and I can resend the file with the correct configuration, but then the job is marked as failed with error, which doesn't feel right to do.

So my question is; what would be the best approach to tell from a media file if it's a video or audio only? It's possible that ffmpeg or ffprobe can do it remotely, but I just don't know the tools deeply enough, so I'm not entirely sure that it can only work in that way I described it.


Solution

  • You could use

    ffprobe -v quiet -print_format json -show_format -show_streams sample.mp4
    

    The above command outputs input file's (here: sample.mp4) parameters along with audio and video streams' details. You can see the sample output here.

    What you're interested in is .streams[].codec_type to figure out if the input file contains only audio, only video or both streams. You can use jq for that matter, for example:

    ffprobe -v quiet -print_format json -show_format -show_streams sample.mp4 | jq .streams[].codec_type
    "video"
    "audio"
    

    or parse the output JSON in any other way.