feat(user/rand_media_pl.sh):Script to play random position of media
[BK-2020-03.git] / unitproc / draft / bktemp-get_av_format
1 #!/usr/bin/env bash
2
3 get_av_format() {
4 # Desc: Gets audio/video format of file as string
5 # Usage: get_av_format arg1
6 # Depends: ffprobe
7 # Version: 0.0.1
8 # Input: arg1: input file path
9 # Output: stdout (if valid audio or video format)
10 # exit code 0 if av file; 1 otherwise
11 # Example: get_av_format myvideo.mp4
12 # Note: Would return "opus" if full ffprobe report had 'Audio: opus, 48000 Hz, stereo, fltp'
13 # Note: Not tested with videos containing multiple video streams
14 # Ref/Attrib: [1] https://stackoverflow.com/questions/5618363/is-there-a-way-to-use-ffmpeg-to-determine-the-encoding-of-a-file-before-transcod
15 # [2] https://stackoverflow.com/questions/44123532/how-to-find-out-the-file-extension-for-extracting-audio-tracks-with-ffmpeg-and-p#comment88464070_50723126
16 # TODO 2022-08-19: output path of validated file to stdout for gnu parallel use
17 local av_format file_in;
18 local return_state;
19 file_in="$1";
20
21 # Return error exit code if not audio file
22 ## Return error if ffprobe itself exited on error
23 if ! ffprobe -v error -show_entries stream=codec_name -of default=nokey=1:noprint_wrappers=1 "$file_in" 1>/dev/null 2>&1; then
24 return_state="false";
25 fi;
26
27 # Get av format
28 av_format="$(ffprobe -v error -select_streams a -show_entries stream=codec_name -of default=nokey=1:noprint_wrappers=1 "$file_in")"; # see [1]
29
30 ## Return error if audio av is incorrectly formatted (e.g. reject if contains spaces)
31 pattern="^[[:alnum:]]+$"; # alphanumeric string with no spaces
32 if [[ $av_format =~ $pattern ]]; then
33 return_state="true";
34 # Report audio format
35 echo "$av_format";
36 else
37 return_state="false";
38 fi;
39
40 # Report exit code
41 if [[ $return_state = "true" ]]; then
42 return 0;
43 # echo "$1" #TODO_______________
44 else
45 return 1;
46 fi;
47 } # Get audio format as stdout
48
49 # Author: Steven Baltakatei Sandoval
50 # License: GPLv3+