Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for all known video containers with fallback to AVI #516

Merged
merged 1 commit into from Nov 30, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 30 additions & 5 deletions app/assets/javascripts/uploadcare/widget/tabs/camera-tab.coffee
Expand Up @@ -166,17 +166,18 @@ uploadcare.namespace 'widget.tabs', (ns) ->
@__setState('recording')

@__chunks = []
__recorderOptions = {
mimeType: 'video/webm'
}
__recorderOptions = {}

if @settings.audioBitsPerSecond != null
__recorderOptions.audioBitsPerSecond = @settings.audioBitsPerSecond

if @settings.videoBitsPerSecond != null
__recorderOptions.videoBitsPerSecond = @settings.videoBitsPerSecond

@__recorder = new @MediaRecorder(@__stream, __recorderOptions)
if Object.keys(__recorderOptions).length != 0
@__recorder = new @MediaRecorder(@__stream, __recorderOptions)
else
@__recorder = new @MediaRecorder(@__stream)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restore the original safe code since MDN says there are browsers who don't support the second argument.


@__recorder.start()
@__recorder.ondataavailable = (e) =>
Expand All @@ -187,7 +188,8 @@ uploadcare.namespace 'widget.tabs', (ns) ->

@__recorder.onstop = =>
blob = new Blob(@__chunks, {'type': @__recorder.mimeType})
blob.name = "record.webm"
ext = @__guessExtensionByMime(@__recorder.mimeType)
blob.name = "record.#{ext}"
@dialogApi.addFiles('object', [[blob, {source: 'camera'}]])
@dialogApi.switchTab('preview')
@__chunks = []
Expand All @@ -198,3 +200,26 @@ uploadcare.namespace 'widget.tabs', (ns) ->

@__recorder.stop()
@__chunks = []

__guessExtensionByMime: (mime) ->
known_containers = {
'mp4': 'mp4',
'ogg': 'ogg',
'webm': 'webm',
'quicktime': 'mov',
'x-matroska': 'mkv',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are all popular video containers. A container (not codecs) defines actual file extension.

}
# e.g. "video/x-matroska;codecs=avc1,opus"
if mime
# e.g. ["video", "x-matroska;codecs=avc1,opus"]
mime = mime.split('/')
if mime[0] == 'video'
# e.g. "x-matroska;codecs=avc1,opus"
mime = mime.slice(1).join('/')
# e.g. "x-matroska"
container = mime.split(';')[0]
# e.g. "mkv"
if known_containers[container]
return known_containers[container]
# In all other cases just return the base extension for all times
return 'avi'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fallback to well known avi to prevent awful extension like x-matroska;codecs=avc1,opus in the future.