Created
June 5, 2022 17:15
-
-
Save theareba/ee245212b805de2145d9c724cbf146fb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class VideoAttachment < Attachment | |
validate :file_codec_validation, if: :new_record? | |
# Checks video file Codec format for Instagram | |
# Fails if not HEVC or H264 | |
# @see https://developers.facebook.com/docs/instagram-api/reference/ig-user/media#video-specifications | |
# @see https://github.com/ruby-av/av/blob/master/lib/av/commands/base.rb | |
def file_codec_validation | |
ffmpeg = `if command -v ffmpeg 2>/dev/null; then echo "true"; else echo "false"; fi` | |
av_probe = `if command -v avprobe 2>/dev/null; then echo "true"; else echo "false"; fi` | |
command = if ffmpeg =~ /true/ | |
'ffmpeg' | |
elsif av_probe =~ /true/ | |
'avconv' | |
end | |
return if command.nil? | |
return if ::Av.run(%(#{command} -i "#{File.expand_path(file.queued_for_write[:original].path)}" 2>&1), [0, 1]) | |
.force_encoding('UTF-8').encode('UTF-8', invalid: :replace) | |
.split("\n").detect { |line| line =~ /Video:(.*)/ }.match(/hevc|h264/) | |
errors.add(:file, 'video codec is not allowed. Must be HEVC or H264') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment