Skip to content

Commit

Permalink
Update amagalma_Explode video items to separate audio and video items…
Browse files Browse the repository at this point in the history
… in separate tracks.lua
  • Loading branch information
amagalma authored Nov 19, 2024
1 parent f50dcaa commit 4df6b82
Showing 1 changed file with 66 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
-- @description Explode video items to separate audio and video items in separate tracks
-- @author amagalma
-- @version 1.00
-- @version 1.20
-- @donation https://www.paypal.me/amagalma
-- @changelog - Force all video takes in selected items to be video-only, and the copies in the added tracks to be audio-only
-- @about
-- - Explodes each selected item into separate items for the video and the audio part. Original items are turned into video-only items, and the new audio--only items are placed in a new track underneath.
-- - Explodes each selected video item into separate items for the video and the audio part. Original items are turned into video-only items, and the new audio-only items are placed in a new track underneath.
-- - Supports items with any number of takes
-- - Only selected items with at least one video take will be processed
-- - Undo will be created only if something is changed
--
-- Inside the script you can specify:
-- 1) the name for the new audio-only tracks
-- 2) the suffix for the video-only items
-- 3) the suffix for the audio-only items
-- 4) whether to group video-only and audio-only items together
-- 1) the name for the new audio-only tracks
-- 2) the suffix for the video-only items
-- 3) the suffix for the audio-only items
-- 4) whether to group video-only and audio-only items together

name_for_new_audio_only_tracks = " -- video Audio only"
suffix_for_video_only_items = " VIDEO"
suffix_for_audio_only_items = " AUDIO"
group_video_and_audio_items_together = true
local name_for_new_audio_only_tracks = " -- video Audio only"
local suffix_for_video_only_items = " VIDEO"
local suffix_for_audio_only_items = " AUDIO"
local group_video_and_audio_items_together = true

---------------------------------------------------------------

Expand All @@ -23,25 +27,31 @@ if sel_item_cnt == 0 then
return reaper.defer(function() end)
end

local videoitems_by_track = {}
local videoitems_per_track = {}
local itemCount_per_track = {}
local tracks_n = 0
local takeGUIDsAndAddresses_by_item = {}
local new_items = {}
local item_pairs = {}
local item_pairs_n = 0

-- Get video items
for i = 0, sel_item_cnt - 1 do
local item = reaper.GetSelectedMediaItem(0, i)
local take = reaper.GetActiveTake(item)
if take then
local src = reaper.GetMediaItemTake_Source( take )
if reaper.GetMediaSourceType( src ) == "VIDEO" then
local track = reaper.GetMediaItem_Track(item)
if not videoitems_by_track[track] then
videoitems_by_track[track] = {}
tracks_n = tracks_n + 1
local take_cnt = reaper.CountTakes( item )
if take_cnt ~= 0 then
for i = 0, take_cnt-1 do
local take = reaper.GetTake( item, i )
local src = reaper.GetMediaItemTake_Source( take )
if reaper.GetMediaSourceType( src ) == "VIDEO" then
local track = reaper.GetMediaItem_Track(item)
if not videoitems_per_track[track] then
videoitems_per_track[track] = {}
tracks_n = tracks_n + 1
itemCount_per_track[track] = 0
end
itemCount_per_track[track] = itemCount_per_track[track] + 1
videoitems_per_track[track][itemCount_per_track[track]] = item
break
end
videoitems_by_track[track][#videoitems_by_track[track]+1] = item
local _, take_guid = reaper.GetSetMediaItemTakeInfo_String( take, "GUID", "", false )
takeGUIDsAndAddresses_by_item[item] = {take_guid, take}
end
end
end
Expand All @@ -50,32 +60,37 @@ if tracks_n == 0 then
return reaper.defer(function() end)
end

local function DisableAudioOrVideo(item,take_guid,audio,take)

local function DisableAudioOrVideo( item, disable_what )
-- item must be video item
-- if audio == true then disable audio else disable video
-- if what == "audio" then disable audio else disable video
local audio = disable_what:lower() == "audio"
local command = audio and "AUDIO 0" or "VIDEO_DISABLED"
local take_guid = "GUID " .. take_guid
local search_guid = true
local _, chunk = reaper.GetItemStateChunk( item, "", false )
local t, t_n = {}, 0
local take_guids = {}
for line in chunk:gmatch("[^\r\n]+") do
t_n = t_n + 1
if search_guid then
if line == take_guid then
search_guid = false
end
elseif search_guid == false then
if line == "<SOURCE VIDEO" then
t[t_n] = line
t_n = t_n + 1
line = command
search_guid = nil
if line == "<SOURCE VIDEO" then
t[t_n] = line
-- get GUID for renaming
for i = t_n-1, 1, -1 do
local guid = t[i]:match("GUID (.+)")
if guid then
take_guids[guid] = true
break
end
end
t_n = t_n + 1
line = command
elseif line == "AUDIO 0" or line == "VIDEO_DISABLED" then
line = ""
end
t[t_n] = line
end
reaper.SetItemStateChunk(item, table.concat(t, "\n"), false)
if take then
for guid in pairs(take_guids) do
local take = reaper.GetMediaItemTakeByGUID( 0, guid )
local name = reaper.GetTakeName(take)
local suffix = audio and suffix_for_video_only_items or suffix_for_audio_only_items
reaper.GetSetMediaItemTakeInfo_String( take, "P_NAME", name .. suffix, true )
Expand All @@ -89,8 +104,8 @@ reaper.PreventUIRefresh(1)
local cur_pos = reaper.GetCursorPosition()
local ar_st, ar_en = reaper.GetSet_ArrangeView2(0,0,0,0,0,0)

-- Do the thing
for track, items in pairs(videoitems_by_track) do
-- Copy the video items to an inserted track below
for track, items in pairs(videoitems_per_track) do
local track_id = reaper.GetMediaTrackInfo_Value( track, "IP_TRACKNUMBER" )
reaper.InsertTrackInProject( 0, track_id, 1 )
local newtrack = reaper.GetTrack( 0, track_id )
Expand All @@ -101,30 +116,31 @@ for track, items in pairs(videoitems_by_track) do
local item = items[i]
reaper.SetMediaItemSelected( item, true )
reaper.Main_OnCommand(40698, 0) -- Copy items
-- Disable audio
local take_guid = takeGUIDsAndAddresses_by_item[item][1]
local take = takeGUIDsAndAddresses_by_item[item][2]
DisableAudioOrVideo(item,take_guid,true,take)
-- Paste new item
reaper.Main_OnCommand(41173, 0) -- Move cursor to start of items
reaper.Main_OnCommand(42398, 0) -- Paste items/tracks
local new_item = reaper.GetSelectedMediaItem(0,0)
new_items[#new_items+1] = new_item
take = reaper.GetActiveTake(new_item)
DisableAudioOrVideo(new_item,take_guid,false,take)
item_pairs_n = item_pairs_n + 1
item_pairs[item_pairs_n] = { item, new_item }
if group_video_and_audio_items_together then
reaper.SetMediaItemSelected( item, true )
reaper.Main_OnCommand(40032, 0) -- Group items
end
end
end

-- Apply the settings
for i = 1, item_pairs_n do
DisableAudioOrVideo( item_pairs[i][1], "audio" )
DisableAudioOrVideo( item_pairs[i][2], "video" )
end

-- Restore view
reaper.SetEditCurPos(cur_pos,false,false)
reaper.GetSet_ArrangeView2(0,1,0,0,ar_st, ar_en)
reaper.SelectAllMediaItems(0, false)
for i = 1, #new_items do
reaper.SetMediaItemSelected(new_items[i],true)
for i = 1, #item_pairs do
reaper.SetMediaItemSelected(item_pairs[i][1],true)
end

reaper.PreventUIRefresh(-1)
Expand Down

0 comments on commit 4df6b82

Please sign in to comment.