Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davehorner/c38b235e995268b7631ae1864e66fdea to your computer and use it in GitHub Desktop.
Save davehorner/c38b235e995268b7631ae1864e66fdea to your computer and use it in GitHub Desktop.

Revisions

  1. @Zehkul Zehkul renamed this gist Jul 2, 2015. 1 changed file with 0 additions and 0 deletions.
  2. @Zehkul Zehkul renamed this gist Jul 2, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @Zehkul Zehkul revised this gist Jun 15, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    This script for mpv intends to offer the fastest and simplest way to convert parts of a video—while you’re watching it and not really more work intensive than making a screenshot.
    A short demonstration:
    http://s1.webmshare.com/dX3BL.webm
    https://d.maxfile.ro/omdwzyhkoa.webm

    ##Installation:

  4. @Zehkul Zehkul revised this gist Jun 12, 2015. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion convert_script.lua
    Original file line number Diff line number Diff line change
    @@ -293,7 +293,9 @@ function encode(enc)
    else
    encode_options = encode_options .. options.libvpx_vp9_options
    --libvpx-vp9 produces slightly bigger files
    bitrate = bitrate * 0.93
    if bitrate then
    bitrate = bitrate * 0.93
    end
    end


  5. @Zehkul Zehkul revised this gist Jun 12, 2015. 1 changed file with 11 additions and 2 deletions.
    13 changes: 11 additions & 2 deletions convert_script.lua
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,7 @@ local options = {
    libvpx_threads = 4, -- libvpx only
    crop_individually = true,
    libvpx_options = "--ovcopts-add=cpu-used=0,auto-alt-ref=1,lag-in-frames=25,quality=good",
    libvpx_vp9_options = "",
    legacy_yad = false, -- if you don’t want to upgrade to at least yad 0.18
    libvpx_fps = "--oautofps", -- --ofps=24000/1001 for example
    audio_bitrate = 112, -- mpv default, in kbps
    @@ -286,8 +287,16 @@ function encode(enc)
    end
    local encode_options = ' ' .. ovc_c
    if ovc == "libvpx" or ovc == "libvpx-vp9" then
    encode_options = encode_options .. ' ' .. options.libvpx_fps .. ' ' .. options.libvpx_options
    .. ' --ovcopts-add=threads=' .. options.libvpx_threads
    encode_options = encode_options .. ' ' .. options.libvpx_fps .. ' --ovcopts-add=threads=' .. options.libvpx_threads .. ' '
    if ovc == "libvpx" then
    encode_options = encode_options .. options.libvpx_options
    else
    encode_options = encode_options .. options.libvpx_vp9_options
    --libvpx-vp9 produces slightly bigger files
    bitrate = bitrate * 0.93
    end


    end
    if twopass then
    encode_options = encode_options .. ' --ovcopts-add=b=' .. bitrate
  6. @Zehkul Zehkul revised this gist Jun 12, 2015. 1 changed file with 14 additions and 10 deletions.
    24 changes: 14 additions & 10 deletions convert_script.lua
    Original file line number Diff line number Diff line change
    @@ -285,7 +285,7 @@ function encode(enc)
    ovc_c = ' '
    end
    local encode_options = ' ' .. ovc_c
    if ovc == "libvpx" then
    if ovc == "libvpx" or ovc == "libvpx-vp9" then
    encode_options = encode_options .. ' ' .. options.libvpx_fps .. ' ' .. options.libvpx_options
    .. ' --ovcopts-add=threads=' .. options.libvpx_threads
    end
    @@ -295,7 +295,7 @@ function encode(enc)
    if not (ovc == "gif") then
    encode_options = encode_options .. ' --ovcopts-add=crf=' .. tostring(crf)
    end
    if ovc == "libvpx" then
    if ovc == "libvpx" or ovc == "libvpx-vp9" then
    encode_options = encode_options .. ' --ovcopts-add=b=10000000'
    end
    end
    @@ -454,24 +454,26 @@ function call_gui ()
    end

    if yad_table[7] == "vp8/webm" then
    format_dropdown_content = "'^vp8/webm!h264/mkv!h264/mp4!h265/mkv!gif!stream copy/mkv'"
    format_dropdown_content = "'^vp8/webm!vp9/webm!h264/mkv!h264/mp4!h265/mkv!gif!stream copy/mkv'"
    elseif yad_table[7] == "vp9/webm" then
    format_dropdown_content = "'vp8/webm!^vp9/webm!h264/mkv!h264/mp4!h265/mkv!gif!stream copy/mkv'"
    elseif yad_table[7] == "h264/mkv" then
    format_dropdown_content = "'vp8/webm!^h264/mkv!h264/mp4!h265/mkv!gif!stream copy/mkv'"
    format_dropdown_content = "'vp8/webm!vp9/webm!^h264/mkv!h264/mp4!h265/mkv!gif!stream copy/mkv'"
    elseif yad_table[7] == "h264/mp4" then
    format_dropdown_content = "'vp8/webm!h264/mkv!^h264/mp4!h265/mkv!gif!stream copy/mkv'"
    format_dropdown_content = "'vp8/webm!vp9/webm!h264/mkv!^h264/mp4!h265/mkv!gif!stream copy/mkv'"
    elseif yad_table[7] == "h265/mkv" then
    format_dropdown_content = "'vp8/webm!h264/mkv!h264/mp4!^h265/mkv!gif!stream copy/mkv'"
    format_dropdown_content = "'vp8/webm!vp9/webm!h264/mkv!h264/mp4!^h265/mkv!gif!stream copy/mkv'"
    elseif yad_table[7] == "gif" then
    format_dropdown_content = "'vp8/webm!h264/mkv!h264/mp4!h265/mkv!^gif!stream copy/mkv'"
    format_dropdown_content = "'vp8/webm!vp9/webm!h264/mkv!h264/mp4!h265/mkv!^gif!stream copy/mkv'"
    elseif yad_table[7] == "stream copy/mkv" then
    format_dropdown_content = "'vp8/webm!h264/mkv!h264/mp4!h265/mkv!gif!^stream copy/mkv'"
    format_dropdown_content = "'vp8/webm!vp9/webm!h264/mkv!h264/mp4!h265/mkv!gif!^stream copy/mkv'"
    else
    format_dropdown_content = "'^vp8/webm!h264/mkv!h264/mp4!h265/mkv!gif!stream copy/mkv'"
    format_dropdown_content = "'^vp8/webm!vp9/webm!h264/mkv!h264/mp4!h265/mkv!gif!stream copy/mkv'"
    end

    else
    mode_dropdown_content = "'Target file size (KiB)!Target file size (MiB)!CRF'"
    format_dropdown_content = "'vp8/webm!h264/mkv!h264/mp4!h265/mkv!gif!stream copy/mkv'"
    format_dropdown_content = "'vp8/webm!vp9/webm!h264/mkv!h264/mp4!h265/mkv!gif!stream copy/mkv'"
    yad_offset_10 = -2
    end

    @@ -624,6 +626,8 @@ function call_gui ()
    ovc = "libx265"
    elseif yad_table[7]:find("vp8") then
    ovc = "libvpx"
    elseif yad_table[7]:find("vp9") then
    ovc = "libvpx-vp9"
    elseif yad_table[7]:find("gif") then
    ovc = "gif"
    extension = ".gif"
  7. @Zehkul Zehkul revised this gist Apr 18, 2015. 2 changed files with 22 additions and 3 deletions.
    4 changes: 4 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -31,6 +31,10 @@ You can bind it to another hotkey in your input.conf with
    ``<key> script_message convert_script``
    and set some options in ``mpv/lua-settings/convert_script.conf`` or with ``--script-opts`` (mpv 0.7 and earlier: ``--lua-opts``)

    audio_bitrate
    Default: 112
    In kbps. The script tries to compensate for audio when going for a file size limit (2pass). You’ll only need to change this if you modify mpv’s audio encoding options, for example with oacopts-add=qscale=0 in the [encoding] section of your mpv.conf (in which case this option would need 64).

    bitrate_multiplier
    Default: 0.975
    To make sure the file won’t go over the target file size, set it to 1 if you don’t care.
    21 changes: 18 additions & 3 deletions convert_script.lua
    Original file line number Diff line number Diff line change
    @@ -14,6 +14,7 @@ local options = {
    libvpx_options = "--ovcopts-add=cpu-used=0,auto-alt-ref=1,lag-in-frames=25,quality=good",
    legacy_yad = false, -- if you don’t want to upgrade to at least yad 0.18
    libvpx_fps = "--oautofps", -- --ofps=24000/1001 for example
    audio_bitrate = 112, -- mpv default, in kbps
    }

    read_options(options, "convert_script")
    @@ -191,6 +192,7 @@ function preparations()
    local filename_ext = mp.get_property_osd("media-title")
    filename_ext = string.gsub(filename_ext, "'", "'\\''")
    local filename = string.gsub(filename_ext, "%....$","")
    metadata_title = filename

    if string.len(filename) > 230 then

    @@ -224,6 +226,13 @@ function encode(enc)
    aspect_first = rect_width / rect_height
    width_first = rect_width
    height_first = rect_height
    if not no_scale then
    if scale_width then
    width_first = scale
    else
    height_first = scale
    end
    end
    end
    rect_width, rect_height = nil
    end
    @@ -332,10 +341,12 @@ function encode(enc)
    end
    local delay = framestep * 4
    if ovc == "gif" then
    full_command = full_command .. ' --vf-add=lavfi=graph=\\"framestep=' .. framestep .. '\\" && convert ' .. tmpfolder .. '/*.png -set delay ' .. delay .. ' -loop 0 -fuzz ' .. fuzz .. '% ' .. dither .. ' -layers optimize '
    full_command = full_command .. ' --vf-add=lavfi=graph=\\"framestep=' .. framestep .. '\\" && convert '
    .. tmpfolder .. '/*.png -set delay ' .. delay .. ' -loop 0 -fuzz ' .. fuzz .. '% ' .. dither .. ' -layers optimize '
    .. full_output_path .. ' && rm -rf ' .. tmpfolder .. ' && notify-send "Gif done") & disown'
    else
    full_command = full_command .. ' && notify-send "Encoding done") & disown'
    full_command = full_command .. ' && notify-send "Encoding done"; mkvpropedit '
    .. full_output_path .. ' -s title="' .. metadata_title .. '") & disown'
    end


    @@ -625,7 +636,11 @@ function call_gui ()
    factor = 1048576
    end
    if twopass then
    bitrate = math.floor(yad_table[6]*factor*8/segments_length*options.bitrate_multiplier)
    local total_bitrate = yad_table[6] * factor
    if audio == "" then
    total_bitrate = total_bitrate - ( options.audio_bitrate * 1000 * segments_length / 8 )
    end
    bitrate = math.floor(total_bitrate*8/segments_length*options.bitrate_multiplier)
    else
    crf = yad_table[6]
    end
  8. @Zehkul Zehkul revised this gist Apr 1, 2015. 1 changed file with 0 additions and 6 deletions.
    6 changes: 0 additions & 6 deletions convert_script.lua
    Original file line number Diff line number Diff line change
    @@ -254,7 +254,6 @@ function encode(enc)
    end
    local sub_auto = mp.get_property("options/sub-auto")
    local sub_delay = mp.get_property("sub-delay")
    local colormatrix_input_range = mp.get_property_native("colormatrix-input-range")
    local hr_seek_demuxer_offset = mp.get_property_native("options/hr-seek-demuxer-offset")

    preparations()
    @@ -270,11 +269,6 @@ function encode(enc)
    .. ' --sub-delay=' .. sub_delay .. ' --sub-auto=' .. sub_auto .. aid
    .. ' --vf-add=' .. vf .. ' --hr-seek-demuxer-offset=' .. hr_seek_demuxer_offset
    .. ' --af=' .. af .. ' ' .. audio_file
    if (ovc == "libvpx") and (colormatrix_input_range == "full") then
    mpv_options = mpv_options .. ' --vf-add=lavfi=graph=[pp=autolevels]'
    else
    mpv_options = mpv_options .. ' --colormatrix-input-range=' .. colormatrix_input_range
    end

    if not (ovc == "gif") then
    ovc_c = ' --ovc=' .. ovc
  9. @Zehkul Zehkul revised this gist Jan 23, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions convert_script.lua
    Original file line number Diff line number Diff line change
    @@ -339,9 +339,9 @@ function encode(enc)
    local delay = framestep * 4
    if ovc == "gif" then
    full_command = full_command .. ' --vf-add=lavfi=graph=\\"framestep=' .. framestep .. '\\" && convert ' .. tmpfolder .. '/*.png -set delay ' .. delay .. ' -loop 0 -fuzz ' .. fuzz .. '% ' .. dither .. ' -layers optimize '
    .. full_output_path .. ' && rm -rf ' .. tmpfolder .. ' && notify-send "Gif done") &; disown'
    .. full_output_path .. ' && rm -rf ' .. tmpfolder .. ' && notify-send "Gif done") & disown'
    else
    full_command = full_command .. ' && notify-send "Encoding done") &; disown'
    full_command = full_command .. ' && notify-send "Encoding done") & disown'
    end


  10. @Zehkul Zehkul revised this gist Jan 23, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion convert_script.lua
    Original file line number Diff line number Diff line change
    @@ -341,7 +341,7 @@ function encode(enc)
    full_command = full_command .. ' --vf-add=lavfi=graph=\\"framestep=' .. framestep .. '\\" && convert ' .. tmpfolder .. '/*.png -set delay ' .. delay .. ' -loop 0 -fuzz ' .. fuzz .. '% ' .. dither .. ' -layers optimize '
    .. full_output_path .. ' && rm -rf ' .. tmpfolder .. ' && notify-send "Gif done") &; disown'
    else
    full_command = full_command .. ' && notify-send "Encoding done") &'
    full_command = full_command .. ' && notify-send "Encoding done") &; disown'
    end


  11. @Zehkul Zehkul revised this gist Jan 23, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion convert_script.lua
    Original file line number Diff line number Diff line change
    @@ -339,7 +339,7 @@ function encode(enc)
    local delay = framestep * 4
    if ovc == "gif" then
    full_command = full_command .. ' --vf-add=lavfi=graph=\\"framestep=' .. framestep .. '\\" && convert ' .. tmpfolder .. '/*.png -set delay ' .. delay .. ' -loop 0 -fuzz ' .. fuzz .. '% ' .. dither .. ' -layers optimize '
    .. full_output_path .. ' && rm -rf ' .. tmpfolder .. ' && notify-send "Gif done") &'
    .. full_output_path .. ' && rm -rf ' .. tmpfolder .. ' && notify-send "Gif done") &; disown'
    else
    full_command = full_command .. ' && notify-send "Encoding done") &'
    end
  12. @Zehkul Zehkul revised this gist Jan 23, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion convert_script.lua
    Original file line number Diff line number Diff line change
    @@ -290,7 +290,7 @@ function encode(enc)
    encode_options = encode_options .. ' --ovcopts-add=b=' .. bitrate
    else
    if not (ovc == "gif") then
    encode_options = encode_options .. ' --ovcopts-add=crf=' .. crf
    encode_options = encode_options .. ' --ovcopts-add=crf=' .. tostring(crf)
    end
    if ovc == "libvpx" then
    encode_options = encode_options .. ' --ovcopts-add=b=10000000'
  13. @Zehkul Zehkul revised this gist Jan 21, 2015. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions convert_script.lua
    Original file line number Diff line number Diff line change
    @@ -217,8 +217,8 @@ function encode(enc)
    end
    else
    -- odd numbers for video resolution are a bad idea
    rect_width = round(rect_width) - (math.mod(round(rect_width), 2))
    rect_height = round(rect_height) - (math.mod(round(rect_height), 2))
    rect_width = round(rect_width) - (math.fmod(round(rect_width), 2))
    rect_height = round(rect_height) - (math.fmod(round(rect_height), 2))
    crop = rect_width .. ":" .. rect_height .. ":" .. round(rect_x1) .. ":" .. round(rect_y1)
    if not aspect_first then
    aspect_first = rect_width / rect_height
    @@ -363,10 +363,10 @@ function encode(enc)

    if scale_width then
    local scale_2 = round(scale / aspect_first)
    scale = scale .. ":" .. (scale_2 - (math.mod(scale_2, 2)))
    scale = scale .. ":" .. (scale_2 - (math.fmod(scale_2, 2)))
    else
    local scale_2 = round(scale * aspect_first)
    scale = (scale_2 - math.mod(scale_2, 2)) .. ":" .. scale
    scale = (scale_2 - math.fmod(scale_2, 2)) .. ":" .. scale
    end
    if no_scale and not width_first then
    scale = dwidth .. ":" .. dheight
    @@ -567,7 +567,7 @@ function call_gui ()
    yad_ret = -2
    end

    scale = tonumber(yad_table[1]) - (math.mod(tonumber(yad_table[1]), 2 ))
    scale = tonumber(yad_table[1]) - (math.fmod(tonumber(yad_table[1]), 2 ))
    scale_sav = scale
    if (yad_table[2] == "FALSE") and (tonumber(yad_table[1]) > 0) then
    scale_width = false
  14. @Zehkul Zehkul revised this gist Jan 18, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions convert_script.lua
    Original file line number Diff line number Diff line change
    @@ -490,7 +490,7 @@ function call_gui ()
    end


    local yad_command = [[LANG=en_US.utf8 yad --title="Convert Script" --center --form --fixed --always-print-result \
    local yad_command = [[LC_NUMERIC=C yad --title="Convert Script" --center --form --fixed --always-print-result \
    --name "convert script" --class "Convert Script" --field="Resize to height:NUM" "]] .. scale_sav --yad_table 1
    .. [[" --field="Resize to width instead:CHK" ]] .. resize_to_width_instead .. " " --yad_table 2
    if options.legacy_yad then
    @@ -525,7 +525,7 @@ function call_gui ()
    yad_command = yad_command .. [[ --button="Crop:1" --button="gtk-cancel:2" --button="gtk-ok:0"; ret=$? && echo $ret]]

    if gif_dialog then
    yad_command = [[echo $(LANG=en_US.utf8 yad --title="Gif settings" --name "convert script" --class "Convert Script" \
    yad_command = [[echo $(LC_NUMERIC=C yad --title="Gif settings" --name "convert script" --class "Convert Script" \
    --center --form --always-print-result --separator="…" \
    --field="Fuzz Factor:NUM" '1!0..100!0.5!1' \
    --field="Framestep:NUM" '3!1..3!1' \
  15. @Zehkul Zehkul revised this gist Jan 18, 2015. 2 changed files with 46 additions and 29 deletions.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -61,7 +61,7 @@ and set some options in ``mpv/lua-settings/convert_script.conf`` or with ``--scr

    legacy_yad
    Default: no
    If you don’t want to upgrade to yad 0.18. Slightly less fancy.
    If you don’t want to upgrade your yad. Features like appending segments won’t be available.

    libvpx_fps
    Default: --oautofps
    73 changes: 45 additions & 28 deletions convert_script.lua
    Original file line number Diff line number Diff line change
    @@ -427,38 +427,51 @@ function call_gui ()
    local mode_dropdown_content
    local resize_to_width_instead
    local include_audio
    local yad_offset_10 = 0

    if not scale_sav then
    scale_sav = 540
    end
    if not yad_table[5] then
    yad_table[5] = ""
    end
    if yad_table[5]:find("(MiB)") then
    mode_dropdown_content = "'Target file size (KiB)!^Target file size (MiB)!CRF'"
    elseif yad_table[5]:find("(KiB)") then
    mode_dropdown_content = "'^Target file size (KiB)!Target file size (MiB)!CRF'"
    elseif yad_table[5]:find("CRF") then
    mode_dropdown_content = "'Target file size (KiB)!Target file size (MiB)!^CRF'"
    else
    mode_dropdown_content = "'^Target file size (KiB)!Target file size (MiB)!CRF'"
    end

    if yad_table[7] == "vp8/webm" then
    format_dropdown_content = "'^vp8/webm!h264/mkv!h264/mp4!h265/mkv!gif!stream copy/mkv'"
    elseif yad_table[7] == "h264/mkv" then
    format_dropdown_content = "'vp8/webm!^h264/mkv!h264/mp4!h265/mkv!gif!stream copy/mkv'"
    elseif yad_table[7] == "h264/mp4" then
    format_dropdown_content = "'vp8/webm!h264/mkv!^h264/mp4!h265/mkv!gif!stream copy/mkv'"
    elseif yad_table[7] == "h265/mkv" then
    format_dropdown_content = "'vp8/webm!h264/mkv!h264/mp4!^h265/mkv!gif!stream copy/mkv'"
    elseif yad_table[7] == "gif" then
    format_dropdown_content = "'vp8/webm!h264/mkv!h264/mp4!h265/mkv!^gif!stream copy/mkv'"
    elseif yad_table[7] == "stream copy/mkv" then
    format_dropdown_content = "'vp8/webm!h264/mkv!h264/mp4!h265/mkv!gif!^stream copy/mkv'"

    if not options.legacy_yad then

    if yad_table[5]:find("(MiB)") then
    mode_dropdown_content = "'Target file size (KiB)!^Target file size (MiB)!CRF'"
    elseif yad_table[5]:find("(KiB)") then
    mode_dropdown_content = "'^Target file size (KiB)!Target file size (MiB)!CRF'"
    elseif yad_table[5]:find("CRF") then
    mode_dropdown_content = "'Target file size (KiB)!Target file size (MiB)!^CRF'"
    else
    mode_dropdown_content = "'^Target file size (KiB)!Target file size (MiB)!CRF'"
    end

    if yad_table[7] == "vp8/webm" then
    format_dropdown_content = "'^vp8/webm!h264/mkv!h264/mp4!h265/mkv!gif!stream copy/mkv'"
    elseif yad_table[7] == "h264/mkv" then
    format_dropdown_content = "'vp8/webm!^h264/mkv!h264/mp4!h265/mkv!gif!stream copy/mkv'"
    elseif yad_table[7] == "h264/mp4" then
    format_dropdown_content = "'vp8/webm!h264/mkv!^h264/mp4!h265/mkv!gif!stream copy/mkv'"
    elseif yad_table[7] == "h265/mkv" then
    format_dropdown_content = "'vp8/webm!h264/mkv!h264/mp4!^h265/mkv!gif!stream copy/mkv'"
    elseif yad_table[7] == "gif" then
    format_dropdown_content = "'vp8/webm!h264/mkv!h264/mp4!h265/mkv!^gif!stream copy/mkv'"
    elseif yad_table[7] == "stream copy/mkv" then
    format_dropdown_content = "'vp8/webm!h264/mkv!h264/mp4!h265/mkv!gif!^stream copy/mkv'"
    else
    format_dropdown_content = "'^vp8/webm!h264/mkv!h264/mp4!h265/mkv!gif!stream copy/mkv'"
    end

    else
    format_dropdown_content = "'^vp8/webm!h264/mkv!h264/mp4!h265/mkv!gif!stream copy/mkv'"
    mode_dropdown_content = "'Target file size (KiB)!Target file size (MiB)!CRF'"
    format_dropdown_content = "'vp8/webm!h264/mkv!h264/mp4!h265/mkv!gif!stream copy/mkv'"
    yad_offset_10 = -2
    end




    if (yad_table[2] == "TRUE") then
    resize_to_width_instead = '"true"'
    @@ -501,11 +514,15 @@ function call_gui ()
    .. '--field="Mode::CB" ' .. mode_dropdown_content --yad_table 5
    .. ' --field="Value::NUM" ' .. yad_table[6] --yad_table 6
    .. ' --field="Output format::CB" ' .. format_dropdown_content --yad_table 7
    .. [[ --field="Advanced:FBTN" 'bash -c "echo \"advanced\" && kill -s SIGUSR1 \"$YAD_PID\""' ]]
    if not options.legacy_yad then
    yad_command = yad_command .. [[ --field="Advanced:FBTN" 'bash -c "echo \"advanced\" && kill -s SIGUSR1 \"$YAD_PID\""' ]]
    end
    end
    if not options.legacy_yad then
    yad_command = yad_command
    .. [[--field="Append another segment:FBTN" 'bash -c "echo \"append\" && kill -s SIGUSR1 \"$YAD_PID\""' ]]
    end
    yad_command = yad_command
    .. [[--field="Append another segment:FBTN" 'bash -c "echo \"append\" && kill -s SIGUSR1 \"$YAD_PID\""' ]]
    .. [[--button="Crop:1" --button="gtk-cancel:2" --button="gtk-ok:0"; ret=$? && echo $ret]]
    yad_command = yad_command .. [[ --button="Crop:1" --button="gtk-cancel:2" --button="gtk-ok:0"; ret=$? && echo $ret]]

    if gif_dialog then
    yad_command = [[echo $(LANG=en_US.utf8 yad --title="Gif settings" --name "convert script" --class "Convert Script" \
    @@ -533,7 +550,7 @@ function call_gui ()

    if not gif_dialog then

    yad_ret = tonumber(yad_table[10])
    yad_ret = tonumber(yad_table[10+yad_offset_10])
    if yad_table[1]:find("append") then
    yad_table[1] = string.gsub(yad_table[1], "append\n", "")
    yad_ret = -1
  16. @Zehkul Zehkul revised this gist Jan 18, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ http://s1.webmshare.com/dX3BL.webm
    ##Installation:

    You need:
    - yad (at least 0.18) ([AUR](https://aur.archlinux.org/packages/yad/))
    - yad (at least 0.26) ([AUR](https://aur.archlinux.org/packages/yad/))
    - mpv (at least 0.4)
    - notify-send (optional, to send a notification when the encode is done) (to be found in libnotify on Arch)
    - mkvmerge (optional, for stream copy)
  17. @Zehkul Zehkul revised this gist Jan 18, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion convert_script.lua
    Original file line number Diff line number Diff line change
    @@ -185,7 +185,7 @@ function preparations()

    end
    if options.use_same_dir then
    options.output_directory = utils.split_path(video)
    options.output_directory = "'" .. utils.split_path(video) .. "'"
    end

    local filename_ext = mp.get_property_osd("media-title")
  18. @Zehkul Zehkul revised this gist Jan 18, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions convert_script.lua
    Original file line number Diff line number Diff line change
    @@ -554,10 +554,12 @@ function call_gui ()
    scale_sav = scale
    if (yad_table[2] == "FALSE") and (tonumber(yad_table[1]) > 0) then
    scale_width = false
    no_scale = false
    elseif yad_table[1] == "0.000000" then
    no_scale = true
    else
    scale_width = true
    no_scale = false
    end
    if yad_table[3] == "TRUE" then
    no_scale = true
  19. @Zehkul Zehkul revised this gist Jan 18, 2015. 2 changed files with 7 additions and 7 deletions.
    4 changes: 2 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -56,8 +56,8 @@ and set some options in ``mpv/lua-settings/convert_script.conf`` or with ``--scr
    Whether you want only the last crop argument to count—no—or you want to crop all segments individually—yes. In that case the final video will inherit the aspect ratio of the first segment.

    libvpx_options
    Default: cpu-used=0,auto-alt-ref=1,lag-in-frames=25,quality=good
    Higher quality at the expense of processing time. This is directly handed over to "--ovcopts-add=", so make sure it’s correct.
    Default: --ovcopts-add=cpu-used=0,auto-alt-ref=1,lag-in-frames=25,quality=good
    Higher quality at the expense of processing time.

    legacy_yad
    Default: no
    10 changes: 5 additions & 5 deletions convert_script.lua
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ local options = {
    use_same_dir = false, -- puts the output files where the original video was
    libvpx_threads = 4, -- libvpx only
    crop_individually = true,
    libvpx_options = "cpu-used=0,auto-alt-ref=1,lag-in-frames=25,quality=good",
    libvpx_options = "--ovcopts-add=cpu-used=0,auto-alt-ref=1,lag-in-frames=25,quality=good",
    legacy_yad = false, -- if you don’t want to upgrade to at least yad 0.18
    libvpx_fps = "--oautofps", -- --ofps=24000/1001 for example
    }
    @@ -282,11 +282,8 @@ function encode(enc)
    ovc_c = ' '
    end
    local encode_options = ' ' .. ovc_c
    if not (options.libvpx_options == "") then
    options.libvpx_options = ' --ovcopts-add=' .. options.libvpx_options
    end
    if ovc == "libvpx" then
    encode_options = encode_options .. ' ' .. options.libvpx_fps .. options.libvpx_options
    encode_options = encode_options .. ' ' .. options.libvpx_fps .. ' ' .. options.libvpx_options
    .. ' --ovcopts-add=threads=' .. options.libvpx_threads
    end
    if twopass then
    @@ -530,6 +527,9 @@ function call_gui ()
    yad_table = {}
    local function helper(line) table.insert(yad_table, line) return "" end
    helper((yad:gsub("(.-)|", helper)))
    for i, e in pairs(yad_table) do
    msg.debug(i .. "" .. e)
    end

    if not gif_dialog then

  20. @Zehkul Zehkul revised this gist Jan 17, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions convert_script.lua
    Original file line number Diff line number Diff line change
    @@ -484,10 +484,10 @@ function call_gui ()
    --name "convert script" --class "Convert Script" --field="Resize to height:NUM" "]] .. scale_sav --yad_table 1
    .. [[" --field="Resize to width instead:CHK" ]] .. resize_to_width_instead .. " " --yad_table 2
    if options.legacy_yad then
    yad_command = yad_command .. [[--field="Dont resize at all:CHK" "false" ]]
    yad_command = yad_command .. [[--field="Don't resize at all:CHK" "false" ]]
    else
    yad_command = yad_command
    .. [[--field="Dont resize at all:BTN" "@bash -c 'if ]] .. '[[ "a%1" == "a0.000000" ]]'
    .. [[--field="Don't resize at all:BTN" "@bash -c 'if ]] .. '[[ "a%1" == "a0.000000" ]]'
    .. [[; then printf '\''1:1\n2:false'\''; else printf '\''1:0.000000\n1:@disabled@\n2:@disabled@'\''; fi'" ]]
    end
    yad_command = yad_command .. '--field="Include audio:CHK" ' .. include_audio .. ' ' --yad_table 4
  21. @Zehkul Zehkul revised this gist Jan 17, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion convert_script.lua
    Original file line number Diff line number Diff line change
    @@ -511,7 +511,7 @@ function call_gui ()
    .. [[--button="Crop:1" --button="gtk-cancel:2" --button="gtk-ok:0"; ret=$? && echo $ret]]

    if gif_dialog then
    yad_command = [[echo $(yad --title="Gif settings" --name "convert script" --class "Convert Script" \
    yad_command = [[echo $(LANG=en_US.utf8 yad --title="Gif settings" --name "convert script" --class "Convert Script" \
    --center --form --always-print-result --separator="…" \
    --field="Fuzz Factor:NUM" '1!0..100!0.5!1' \
    --field="Framestep:NUM" '3!1..3!1' \
  22. @Zehkul Zehkul revised this gist Jan 17, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion convert_script.lua
    Original file line number Diff line number Diff line change
    @@ -480,7 +480,7 @@ function call_gui ()
    end


    local yad_command = [[yad --title="Convert Script" --center --form --fixed --always-print-result \
    local yad_command = [[LANG=en_US.utf8 yad --title="Convert Script" --center --form --fixed --always-print-result \
    --name "convert script" --class "Convert Script" --field="Resize to height:NUM" "]] .. scale_sav --yad_table 1
    .. [[" --field="Resize to width instead:CHK" ]] .. resize_to_width_instead .. " " --yad_table 2
    if options.legacy_yad then
  23. @Zehkul Zehkul revised this gist Jan 16, 2015. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions convert_script.lua
    Original file line number Diff line number Diff line change
    @@ -217,8 +217,8 @@ function encode(enc)
    end
    else
    -- odd numbers for video resolution are a bad idea
    rect_width = round(rect_width) - (round(rect_width) % 2)
    rect_height = round(rect_height) - (round(rect_height) % 2)
    rect_width = round(rect_width) - (math.mod(round(rect_width), 2))
    rect_height = round(rect_height) - (math.mod(round(rect_height), 2))
    crop = rect_width .. ":" .. rect_height .. ":" .. round(rect_x1) .. ":" .. round(rect_y1)
    if not aspect_first then
    aspect_first = rect_width / rect_height
    @@ -366,10 +366,10 @@ function encode(enc)

    if scale_width then
    local scale_2 = round(scale / aspect_first)
    scale = scale .. ":" .. (scale_2 - (scale_2 % 2))
    scale = scale .. ":" .. (scale_2 - (math.mod(scale_2, 2)))
    else
    local scale_2 = round(scale * aspect_first)
    scale = (scale_2 - (scale_2 % 2)) .. ":" .. scale
    scale = (scale_2 - math.mod(scale_2, 2)) .. ":" .. scale
    end
    if no_scale and not width_first then
    scale = dwidth .. ":" .. dheight
    @@ -550,7 +550,7 @@ function call_gui ()
    yad_ret = -2
    end

    scale = tonumber(yad_table[1]) - (tonumber(yad_table[1]) % 2)
    scale = tonumber(yad_table[1]) - (math.mod(tonumber(yad_table[1]), 2 ))
    scale_sav = scale
    if (yad_table[2] == "FALSE") and (tonumber(yad_table[1]) > 0) then
    scale_width = false
  24. @Zehkul Zehkul revised this gist Jan 16, 2015. 2 changed files with 10 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -41,7 +41,11 @@ and set some options in ``mpv/lua-settings/convert_script.conf`` or with ``--scr

    use_pwd_instead
    Default: no
    Use your working directory as the output directory. Overrides output_directory.
    Use your working directory as the output directory. Overrides output_directory.

    use_same_dir
    Default: no
    Use the directory of the original video file. Overrides output_directory.

    libvpx_threads
    Default: 4
    5 changes: 5 additions & 0 deletions convert_script.lua
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,14 @@
    local assdraw = require 'mp.assdraw'
    local msg = require 'mp.msg'
    local opt = require 'mp.options'
    local utils = require 'mp.utils'

    -- default options, convert_script.conf is read
    local options = {
    bitrate_multiplier = 0.975, -- to make sure the file won’t go over the target file size, set it to 1 if you don’t care
    output_directory = "$HOME",
    use_pwd_instead = false, -- overrides output_directory
    use_same_dir = false, -- puts the output files where the original video was
    libvpx_threads = 4, -- libvpx only
    crop_individually = true,
    libvpx_options = "cpu-used=0,auto-alt-ref=1,lag-in-frames=25,quality=good",
    @@ -182,6 +184,9 @@ function preparations()
    options.output_directory = "'" .. pwd .. "'"

    end
    if options.use_same_dir then
    options.output_directory = utils.split_path(video)
    end

    local filename_ext = mp.get_property_osd("media-title")
    filename_ext = string.gsub(filename_ext, "'", "'\\''")
  25. @Zehkul Zehkul revised this gist Jan 16, 2015. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion convert_script.lua
    Original file line number Diff line number Diff line change
    @@ -277,8 +277,11 @@ function encode(enc)
    ovc_c = ' '
    end
    local encode_options = ' ' .. ovc_c
    if not (options.libvpx_options == "") then
    options.libvpx_options = ' --ovcopts-add=' .. options.libvpx_options
    end
    if ovc == "libvpx" then
    encode_options = encode_options .. ' ' .. options.libvpx_fps .. ' --ovcopts-add=' .. options.libvpx_options
    encode_options = encode_options .. ' ' .. options.libvpx_fps .. options.libvpx_options
    .. ' --ovcopts-add=threads=' .. options.libvpx_threads
    end
    if twopass then
  26. @Zehkul Zehkul revised this gist Jan 16, 2015. 2 changed files with 514 additions and 89 deletions.
    66 changes: 66 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    #README:

    This script for mpv intends to offer the fastest and simplest way to convert parts of a video—while you’re watching it and not really more work intensive than making a screenshot.
    A short demonstration:
    http://s1.webmshare.com/dX3BL.webm

    ##Installation:

    You need:
    - yad (at least 0.18) ([AUR](https://aur.archlinux.org/packages/yad/))
    - mpv (at least 0.4)
    - notify-send (optional, to send a notification when the encode is done) (to be found in libnotify on Arch)
    - mkvmerge (optional, for stream copy)
    - imagemagick (optional, for gifs)
    - linux

    Install it by putting the script into ~/.config/mpv/scripts/ or ~/.mpv/scripts/ (mpv 0.8+) and ~/.config/mpv/lua/ ~/.mpv/lua/ respectively (mpv 0.4 through 0.7)


    ##Usage:

    ``alt + w`` activates the script. First activation marks one end of the clip, the second one the other, order does not matter.

    Cropping doesn’t really work well with the OSC.


    ##Configuration:

    Configuration works in the same way as [configuring the OSC does](http://mpv.io/manual/master/#configuration).
    You can bind it to another hotkey in your input.conf with
    ``<key> script_message convert_script``
    and set some options in ``mpv/lua-settings/convert_script.conf`` or with ``--script-opts`` (mpv 0.7 and earlier: ``--lua-opts``)

    bitrate_multiplier
    Default: 0.975
    To make sure the file won’t go over the target file size, set it to 1 if you don’t care.

    output_directory
    Default: $HOME
    Where the script saves your converted files.

    use_pwd_instead
    Default: no
    Use your working directory as the output directory. Overrides output_directory.

    libvpx_threads
    Default: 4
    Number of threads to use for encoding, libvpx only because it apparently doesn’t have auto detection.

    crop_individually
    Default: yes
    Whether you want only the last crop argument to count—no—or you want to crop all segments individually—yes. In that case the final video will inherit the aspect ratio of the first segment.

    libvpx_options
    Default: cpu-used=0,auto-alt-ref=1,lag-in-frames=25,quality=good
    Higher quality at the expense of processing time. This is directly handed over to "--ovcopts-add=", so make sure it’s correct.

    legacy_yad
    Default: no
    If you don’t want to upgrade to yad 0.18. Slightly less fancy.

    libvpx_fps
    Default: --oautofps
    FPS settings (or any other settings really) for libvpx encoding. Set it to --ofps=24000/1001 for example.

    Warning: Some of these options aren’t very robust and setting them to bogus values will break the script.
    537 changes: 448 additions & 89 deletions convert_script.lua
    Original file line number Diff line number Diff line change
    @@ -1,25 +1,22 @@
    -- README:
    -- convert script for mpv, just throw it into ~/.mpv/lua/
    -- needs: yad (at least 0.18), libnotify (optional) and at least mpv 0.4
    -- bind it to another hotkey with
    -- <key> script_message convert_script
    -- in your input.conf (standard: alt+w)
    -- might or might not work together with the OSC, and it’s somewhat ugly (flickering)
    -- probably linux only due to yad, no idea if it compiles elsewhere

    local assdraw = require 'mp.assdraw'
    local msg = require 'mp.msg'
    local opt = require 'mp.options'

    -- default options, convert_script.conf is read
    local options = {
    bitrate_multiplier = 0.975, -- to make sure the file won’t go over the target file size, set it to 1 if you don’t care
    output_directory = '"$HOME"',
    output_directory = "$HOME",
    use_pwd_instead = false, -- overrides output_directory
    threads = 4,
    libvpx_threads = 4, -- libvpx only
    crop_individually = true,
    libvpx_options = "cpu-used=0,auto-alt-ref=1,lag-in-frames=25,quality=good",
    legacy_yad = false, -- if you don’t want to upgrade to at least yad 0.18
    libvpx_fps = "--oautofps", -- --ofps=24000/1001 for example
    }

    read_options(options, "convert_script")
    options.output_directory = '"' .. options.output_directory .. '"'
    yad_table = {}

    -------------------------------------
    -- Rectangle selection and drawing --
    @@ -126,7 +123,7 @@ function convert_script_hotkey_call ()

    if timepos1 then

    timepos2 = mp.get_property(playback_time)
    timepos2 = mp.get_property_native(playback_time)
    timepos2_humanreadable = mp.get_property_osd(playback_time)

    if tonumber(timepos1) > tonumber(timepos2) then
    @@ -151,15 +148,15 @@ function convert_script_hotkey_call ()
    mp.osd_message("Both frames are the same, ignoring the second one")
    timepos2 = nil
    return
    end

    timepos1 = nil
    call_gui()

    else

    timepos1 = mp.get_property(playback_time)
    timepos1 = mp.get_property_native(playback_time)
    timepos1_humanreadable = mp.get_property_osd(playback_time)
    msg.info("Start frame set")
    mp.osd_message("Start frame set")
    @@ -173,24 +170,64 @@ end
    -- Encode --
    ------------

    function encode ()
    function preparations()

    video = mp.get_property("path")
    video = string.gsub(video, "'", "'\\''")

    if options.use_pwd_instead then

    local pwd = os.getenv("PWD")
    pwd = string.gsub(pwd, "'", "'\\''")
    options.output_directory = "'" .. pwd .. "'"

    end

    local filename_ext = mp.get_property_osd("media-title")
    filename_ext = string.gsub(filename_ext, "'", "'\\''")
    local filename = string.gsub(filename_ext, "%....$","")

    if string.len(filename) > 230 then

    filename = mp.get_property("options/title")
    if filename == 'mpv - ${media-title}' or string.len(filename) > 230 then
    filename = 'output'
    end

    end

    filename = filename .. " " .. start_humanreadable .. "-" .. end_humanreadable .. extension
    filename = string.gsub(filename, ":", ".")
    full_output_path = options.output_directory .. "/'" .. filename .. "'"

    end

    function encode(enc)

    set_mouse_area = nil
    if rect_width == nil or rect_width == 0 then
    crop = ""
    if not aspect_first then
    aspect_first = mp.get_property_native("video-params/aspect")
    end
    else
    crop = math.floor(rect_width) .. ":" .. math.floor(rect_height) .. ":" .. math.ceil(rect_x1) .. ":" .. math.ceil(rect_y1)
    rect_width, rect_height= nil
    -- odd numbers for video resolution are a bad idea
    rect_width = round(rect_width) - (round(rect_width) % 2)
    rect_height = round(rect_height) - (round(rect_height) % 2)
    crop = rect_width .. ":" .. rect_height .. ":" .. round(rect_x1) .. ":" .. round(rect_y1)
    if not aspect_first then
    aspect_first = rect_width / rect_height
    width_first = rect_width
    height_first = rect_height
    end
    rect_width, rect_height = nil
    end

    local video = mp.get_property("path")
    video = string.gsub(video, "'", "'\\''")
    local sid = mp.get_property("sid")
    local sub_visibility = mp.get_property("sub-visibility")
    local aid = ""
    if not audio == "--no-audio" then
    aid = " --aid=" .. mp.get_property("aid")
    end
    aid = " --aid=" .. mp.get_property("aid")
    local af = mp.get_property("af")
    local vf = mp.get_property("vf")
    if string.len(vf) > 0 then
    vf = vf .. ","
    @@ -200,60 +237,176 @@ function encode ()
    for index, param in pairs(sub_file_table) do
    sub_file = sub_file .. " --sub-file='" .. string.gsub(tostring(param), "'", "'\\''") .. "'"
    end
    local audio_file_table = mp.get_property_native("options/audio-file")
    local audio_file = ""
    for index, param in pairs(audio_file_table) do
    audio_file = audio_file .. " --audio-file='" .. string.gsub(tostring(param), "'", "'\\''") .. "'"
    end
    if mp.get_property_native("mute") then
    audio_file = ""
    aid = ""
    audio = "--no-audio"
    end
    local sub_auto = mp.get_property("options/sub-auto")
    local sub_delay = mp.get_property("sub-delay")
    local colormatrix_input_range = mp.get_property_native("colormatrix-input-range")
    local hr_seek_demuxer_offset = mp.get_property_native("options/hr-seek-demuxer-offset")

    if options.use_pwd_instead then
    preparations()

    local pwd = os.getenv("PWD")
    pwd = string.gsub(pwd, "'", "'\\''")
    options.output_directory = "'" .. pwd .. "'"

    if options.crop_individually then
    vf = vf .. 'sub,crop=' .. crop .. ',scale=SCALE…VAR'
    else
    vf = vf .. 'sub,crop=CROP…VAR,scale=SCALE…VAR'
    end

    local mpv_options = "'" .. video .. "' --start=+" .. start .. ' --length=' .. length
    .. ' ' .. sub_file .. ' --sid=' .. sid .. ' --sub-visibility=' .. sub_visibility
    .. ' --sub-delay=' .. sub_delay .. ' --sub-auto=' .. sub_auto .. aid
    .. ' --vf-add=' .. vf .. ' --hr-seek-demuxer-offset=' .. hr_seek_demuxer_offset
    .. ' --af=' .. af .. ' ' .. audio_file
    if (ovc == "libvpx") and (colormatrix_input_range == "full") then
    mpv_options = mpv_options .. ' --vf-add=lavfi=graph=[pp=autolevels]'
    else
    mpv_options = mpv_options .. ' --colormatrix-input-range=' .. colormatrix_input_range
    end

    local filename_ext = mp.get_property_osd("media-title")
    filename_ext = string.gsub(filename_ext, "'", "'\\''")
    local filename = string.gsub(filename_ext, "%....$","")
    if not (ovc == "gif") then
    ovc_c = ' --ovc=' .. ovc
    else
    ovc_c = ' '
    end
    local encode_options = ' ' .. ovc_c
    if ovc == "libvpx" then
    encode_options = encode_options .. ' ' .. options.libvpx_fps .. ' --ovcopts-add=' .. options.libvpx_options
    .. ' --ovcopts-add=threads=' .. options.libvpx_threads
    end
    if twopass then
    encode_options = encode_options .. ' --ovcopts-add=b=' .. bitrate
    else
    if not (ovc == "gif") then
    encode_options = encode_options .. ' --ovcopts-add=crf=' .. crf
    end
    if ovc == "libvpx" then
    encode_options = encode_options .. ' --ovcopts-add=b=10000000'
    end
    end

    if string.len(filename) > 230 then
    if advanced then
    encode_options = ' ' .. advanced_output .. advanced_encode
    end

    filename = mp.get_property("options/title")
    if filename == 'mpv - ${media-title}' or string.len(filename) > 230 then
    filename = 'output'
    end

    if not segments then
    segments = ""
    segment_count = 0
    end
    segment_count = segment_count + 1

    filename = filename .. " " .. start_humanreadable .. "-" .. end_humanreadable .. ".webm"

    local mpv_options = "mpv '" .. video .. "' --start=+" .. start .. ' --length=' .. length
    .. ' ' .. sub_file .. ' --sid=' .. sid .. ' --sub-visibility=' .. sub_visibility
    .. ' --sub-delay=' .. sub_delay .. ' --sub-auto=' .. sub_auto .. aid
    .. ' --colormatrix-input-range=' .. colormatrix_input_range
    .. ' --vf=' .. vf .. 'sub,crop=' .. crop .. ',scale=' .. scale
    .. ' --hr-seek-demuxer-offset=' .. hr_seek_demuxer_offset
    segments = segments .. " --\\{ " .. mpv_options .. " --\\}"

    local encode_options = ' --oautofps --of=webm --ovc=libvpx' .. ' --ovcopts=b=' .. bitrate
    .. ',cpu-used=0,auto-alt-ref=1,lag-in-frames=25,quality=good,threads=' .. options.threads
    encode_options = encode_options .. ',flags=+pass'
    local full_command = '( mpv' .. segments
    if twopass then
    encode_options = encode_options .. ' --ovcopts-add=flags=+pass'
    full_command = full_command .. ' --no-audio ' .. encode_options .. '1'
    end
    if ovc == "gif" then
    local handle = io.popen("mktemp -d")
    tmpfolder = handle:read("*a")
    handle:close()
    tmpfolder = string.gsub(tmpfolder, '\n', '')
    encode_options = encode_options .. ' --no-keep-open --vo=image:format=png:outdir=' .. tmpfolder
    else
    full_command = full_command .. ' --o=' .. full_output_path
    end
    if twopass then
    full_command = full_command .. ' && mpv' .. segments .. ' ' .. audio .. ' ' .. encode_options .. '2'
    .. ' --o=' .. full_output_path
    .. ' && rm ' .. full_output_path .. '-vo-lavc-pass1.log'
    else
    full_command = full_command .. ' ' .. audio .. ' ' .. encode_options
    end

    local full_output_path = options.output_directory .. "/'" .. filename .. "'"
    if not framestep then
    framestep = 1
    dither = "+dither"
    fuzz = "1%"
    end
    local delay = framestep * 4
    if ovc == "gif" then
    full_command = full_command .. ' --vf-add=lavfi=graph=\\"framestep=' .. framestep .. '\\" && convert ' .. tmpfolder .. '/*.png -set delay ' .. delay .. ' -loop 0 -fuzz ' .. fuzz .. '% ' .. dither .. ' -layers optimize '
    .. full_output_path .. ' && rm -rf ' .. tmpfolder .. ' && notify-send "Gif done") &'
    else
    full_command = full_command .. ' && notify-send "Encoding done") &'
    end


    if enc then

    -- mpv parses --{ --} last and --no-audio gets overwritten by --aid, so remove them
    if audio == "--no-audio" then
    full_command = string.gsub(full_command, "--aid=%d* ", " ")
    end
    local dwidth, dheight = mp.get_property_native("dwidth"), mp.get_property_native("dheight")

    if width_first then
    if scale_width then
    scale = width_first
    else
    scale = height_first
    end
    end

    local full_command = '(' .. mpv_options .. ' --no-audio' .. encode_options
    full_command = full_command .. '1'
    full_command = full_command .. ' --o=' .. full_output_path
    full_command = full_command .. ' && ' .. mpv_options .. ' ' .. audio .. encode_options .. '2'
    .. ' --o=' .. full_output_path
    .. ' && rm ' .. full_output_path .. '-vo-lavc-pass1.log'
    full_command = full_command .. '&& notify-send "Encoding done") &'
    if scale_width then
    local scale_2 = round(scale / aspect_first)
    scale = scale .. ":" .. (scale_2 - (scale_2 % 2))
    else
    local scale_2 = round(scale * aspect_first)
    scale = (scale_2 - (scale_2 % 2)) .. ":" .. scale
    end
    if no_scale and not width_first then
    scale = dwidth .. ":" .. dheight
    end

    full_command = string.gsub(full_command, "CROP…VAR", crop)
    full_command = string.gsub(full_command, "SCALE…VAR", scale)
    msg.info(full_command)
    os.execute(full_command)
    clear()

    end
    end

    function encode_copy(enc)

    preparations()
    if not mkvmerge_parts then
    mkvmerge_parts = "--split parts:"
    sep = ""
    end

    msg.info(full_command)
    os.execute(full_command)
    mkvmerge_parts = mkvmerge_parts .. sep .. math.floor(start) .. "s-" .. math.ceil(start + length) .. "s"
    sep = ",+"

    if enc then
    local command = "mkvmerge '" .. video .. "' " .. mkvmerge_parts .. " -o " .. full_output_path
    msg.info(command)
    os.execute(command)
    clear()
    end

    end

    function clear()

    mkvmerge_parts = nil
    aspect_first = nil
    height_first = nil
    width_first = nil
    sep = nil
    segments = nil
    segments_length = 0

    end

    ---------
    -- GUI --
    @@ -263,56 +416,262 @@ function call_gui ()

    mp.disable_key_bindings("draw_rectangle")
    mp.resume_all()
    local yad_command = ([[yad --title="Convert Script" --center --form --separator="\n" --fixed \
    --field="Resize to height:NUM" "540" \]] --yad_table 1 and 2
    .. [[--field="Resize to width instead:CHK" "false" \]] --yad_table 3
    .. [[--field="Don’t resize at all:BTN" "@bash -c 'if ]] .. '[[ "a%1" == "a0.000000" ]]'
    .. [[; then printf '\''1:1\n2:false'\''; else printf '\''1:0.000000\n1:@disabled@\n2:@disabled@'\''; fi'" \]]
    .. [[--field="Include audio:CHK" "false" \]] --yad_table 4
    .. [[--field="Target file size (kB):NUM" "3072" \]] --yad_table 5
    .. [[--button="Crop:1" --button="gtk-cancel:2" --button="gtk-ok:0" && echo "$?"]])
    local handle = io.popen(yad_command)
    local yad = handle:read("*a")
    handle:close()

    if yad == "" then
    mp.enable_key_bindings("draw_rectangle")
    return
    extension = ""
    local factor = 1024
    local format_dropdown_content
    local mode_dropdown_content
    local resize_to_width_instead
    local include_audio

    if not scale_sav then
    scale_sav = 540
    end
    if not yad_table[5] then
    yad_table[5] = ""
    end
    if yad_table[5]:find("(MiB)") then
    mode_dropdown_content = "'Target file size (KiB)!^Target file size (MiB)!CRF'"
    elseif yad_table[5]:find("(KiB)") then
    mode_dropdown_content = "'^Target file size (KiB)!Target file size (MiB)!CRF'"
    elseif yad_table[5]:find("CRF") then
    mode_dropdown_content = "'Target file size (KiB)!Target file size (MiB)!^CRF'"
    else
    mode_dropdown_content = "'^Target file size (KiB)!Target file size (MiB)!CRF'"
    end

    local yad_table = {}
    if yad_table[7] == "vp8/webm" then
    format_dropdown_content = "'^vp8/webm!h264/mkv!h264/mp4!h265/mkv!gif!stream copy/mkv'"
    elseif yad_table[7] == "h264/mkv" then
    format_dropdown_content = "'vp8/webm!^h264/mkv!h264/mp4!h265/mkv!gif!stream copy/mkv'"
    elseif yad_table[7] == "h264/mp4" then
    format_dropdown_content = "'vp8/webm!h264/mkv!^h264/mp4!h265/mkv!gif!stream copy/mkv'"
    elseif yad_table[7] == "h265/mkv" then
    format_dropdown_content = "'vp8/webm!h264/mkv!h264/mp4!^h265/mkv!gif!stream copy/mkv'"
    elseif yad_table[7] == "gif" then
    format_dropdown_content = "'vp8/webm!h264/mkv!h264/mp4!h265/mkv!^gif!stream copy/mkv'"
    elseif yad_table[7] == "stream copy/mkv" then
    format_dropdown_content = "'vp8/webm!h264/mkv!h264/mp4!h265/mkv!gif!^stream copy/mkv'"
    else
    format_dropdown_content = "'^vp8/webm!h264/mkv!h264/mp4!h265/mkv!gif!stream copy/mkv'"
    end

    local i = 0
    for k in string.gmatch(yad, "[%a%d]+") do
    i = i + 1
    yad_table[i] = k
    if (yad_table[2] == "TRUE") then
    resize_to_width_instead = '"true"'
    else
    resize_to_width_instead = '"false"'
    end

    if (yad_table[3] == "FALSE") and (tonumber(yad_table[1]) > 0) then
    scale = "-3:" .. yad_table[1]
    elseif yad_table[1] == "0" then
    scale = ""
    if (yad_table[4] == "TRUE") then
    include_audio = '"true"'
    else
    scale = yad_table[1] .. ":-3"
    include_audio = '"false"'
    end

    if yad_table[4] == "FALSE" then
    audio = '--no-audio'
    if (not yad_table[6]) then
    yad_table[6] = '"3072"'
    end


    local yad_command = [[yad --title="Convert Script" --center --form --fixed --always-print-result \
    --name "convert script" --class "Convert Script" --field="Resize to height:NUM" "]] .. scale_sav --yad_table 1
    .. [[" --field="Resize to width instead:CHK" ]] .. resize_to_width_instead .. " " --yad_table 2
    if options.legacy_yad then
    yad_command = yad_command .. [[--field="Don’t resize at all:CHK" "false" ]]
    else
    audio = ""
    yad_command = yad_command
    .. [[--field="Don’t resize at all:BTN" "@bash -c 'if ]] .. '[[ "a%1" == "a0.000000" ]]'
    .. [[; then printf '\''1:1\n2:false'\''; else printf '\''1:0.000000\n1:@disabled@\n2:@disabled@'\''; fi'" ]]
    end
    yad_command = yad_command .. '--field="Include audio:CHK" ' .. include_audio .. ' ' --yad_table 4

    bitrate = math.floor(yad_table[5]*1024*8/length*options.bitrate_multiplier)
    if yad_ret then
    yad_command = yad_command
    .. [[--field="2pass:CHK" "false" ]] --yad_table 5
    .. [[--field="Encode options::CBE" '! --ovcopts=b=2000,cpu-used=0,auto-alt-ref=1,lag-in-frames=25,quality=good,threads=4' ]] --yad_table 6
    .. [[--field="Output format::CBE" ' --ovc=libx264! --oautofps --of=webm --ovc=libvpx' ]]
    .. [[--field="Simple:FBTN" 'bash -c "echo \"simple\" && kill -s SIGUSR1 \"$YAD_PID\""' ]]
    advanced = true
    else
    yad_command = yad_command
    .. '--field="Mode::CB" ' .. mode_dropdown_content --yad_table 5
    .. ' --field="Value::NUM" ' .. yad_table[6] --yad_table 6
    .. ' --field="Output format::CB" ' .. format_dropdown_content --yad_table 7
    .. [[ --field="Advanced:FBTN" 'bash -c "echo \"advanced\" && kill -s SIGUSR1 \"$YAD_PID\""' ]]
    end
    yad_command = yad_command
    .. [[--field="Append another segment:FBTN" 'bash -c "echo \"append\" && kill -s SIGUSR1 \"$YAD_PID\""' ]]
    .. [[--button="Crop:1" --button="gtk-cancel:2" --button="gtk-ok:0"; ret=$? && echo $ret]]

    if gif_dialog then
    yad_command = [[echo $(yad --title="Gif settings" --name "convert script" --class "Convert Script" \
    --center --form --always-print-result --separator="…" \
    --field="Fuzz Factor:NUM" '1!0..100!0.5!1' \
    --field="Framestep:NUM" '3!1..3!1' \
    --field="Dither:CB" 'None!E-Dither!Ordered Dither' \
    --button="Ok:0")]]
    end

    local handle = io.popen(yad_command)
    local yad = handle:read("*a")
    handle:close()
    if yad == "127\n" then
    msg.error("Error: Cannot find yad!")
    mp.osd_message("Error: Cannot find yad!")
    return
    end
    yad_table = {}
    local function helper(line) table.insert(yad_table, line) return "" end
    helper((yad:gsub("(.-)|", helper)))

    if not gif_dialog then

    yad_ret = tonumber(yad_table[10])
    if yad_table[1]:find("append") then
    yad_table[1] = string.gsub(yad_table[1], "append\n", "")
    yad_ret = -1
    end
    if yad_table[1]:find("advanced") then
    yad_table[1] = string.gsub(yad_table[1], "advanced\n", "")
    yad_ret = -2
    if yad_table[7]:find("gif") then
    gif_dialog = true
    end
    end
    if yad_table[1]:find("simple") then
    yad_table[1] = string.gsub(yad_table[1], "simple\n", "")
    yad_ret = -2
    end

    scale = tonumber(yad_table[1]) - (tonumber(yad_table[1]) % 2)
    scale_sav = scale
    if (yad_table[2] == "FALSE") and (tonumber(yad_table[1]) > 0) then
    scale_width = false
    elseif yad_table[1] == "0.000000" then
    no_scale = true
    else
    scale_width = true
    end
    if yad_table[3] == "TRUE" then
    no_scale = true
    end

    if yad_table[4] == "FALSE" then
    audio = "--no-audio"
    else
    audio = ""
    end

    if not segments_length then
    segments_length = 0
    end

    segments_length = segments_length + length

    if (yad_table[5] == "TRUE") or yad_table[5]:find("Target") then
    twopass = true
    else
    twopass = false
    end

    if advanced then
    advanced_encode = yad_table[6]
    advanced_output = yad_table[7]
    yad_table[6] = '"3072"'
    else
    if yad_table[7]:find("webm") then
    extension = ".webm"
    elseif yad_table[7]:find("mkv") then
    extension = ".mkv"
    elseif yad_table[7]:find("mp4") then
    extension = ".mp4"
    end

    if yad_table[7]:find("h264") then
    ovc = "libx264"
    elseif yad_table[7]:find("h265") then
    ovc = "libx265"
    elseif yad_table[7]:find("vp8") then
    ovc = "libvpx"
    elseif yad_table[7]:find("gif") then
    ovc = "gif"
    extension = ".gif"
    twopass = false
    elseif yad_table[7]:find("stream copy") then
    ovc = "stream copy"
    end

    if yad_table[5]:find("(MiB)") then
    factor = 1048576
    end
    if twopass then
    bitrate = math.floor(yad_table[6]*factor*8/segments_length*options.bitrate_multiplier)
    else
    crf = yad_table[6]
    end

    end

    if yad_ret == 1 then
    mp.enable_key_bindings("draw_rectangle")
    yad_ret = nil
    return
    end

    else

    gif_dialog = nil
    helper((yad:gsub("(.-)…", helper)))
    fuzz = yad_table[2]
    framestep = yad_table[3]
    if yad_table[4] == "None" then
    dither = "+dither"
    elseif yad_table[4] == "Ordered Dither" then
    dither = "-ordered-dither o8x8,20"
    else
    dither = ""
    end
    yad_table[7] = "gif"

    end


    mp.set_osd_ass(width, height, "")

    if yad_table[7] == "0" then
    encode()
    if yad_ret == 0 then
    if ovc == "stream copy" then
    encode_copy(true)
    else
    encode(true)
    end
    elseif yad_ret == -1 then
    if ovc == "stream copy" then
    encode(false)
    encode_copy(false)
    else
    encode(false)
    encode_copy(false)
    end
    end

    if yad_ret == -2 then
    if advanced then
    advanced = false
    yad_ret = false
    end
    call_gui()
    end

    if yad_ret == 2 then
    clear()
    end
    yad_ret = nil
    advanced = nil

    end


    function round(n)
    return math.floor((math.floor(n*2) + 1)/2)
    end



  27. @Zehkul Zehkul revised this gist Dec 7, 2014. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions convert_script.lua
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    -- README:
    -- convert script for mpv, just throw it into ~/.mpv/lua/
    -- needs: yad, libnotify and at least mpv 0.4
    -- needs: yad (at least 0.18), libnotify (optional) and at least mpv 0.4
    -- bind it to another hotkey with
    -- <key> script_message convert_script
    -- in your input.conf (standard: alt+w)
    @@ -187,7 +187,10 @@ function encode ()
    video = string.gsub(video, "'", "'\\''")
    local sid = mp.get_property("sid")
    local sub_visibility = mp.get_property("sub-visibility")
    local aid = mp.get_property("aid")
    local aid = ""
    if not audio == "--no-audio" then
    aid = " --aid=" .. mp.get_property("aid")
    end
    local vf = mp.get_property("vf")
    if string.len(vf) > 0 then
    vf = vf .. ","
    @@ -227,7 +230,7 @@ function encode ()

    local mpv_options = "mpv '" .. video .. "' --start=+" .. start .. ' --length=' .. length
    .. ' ' .. sub_file .. ' --sid=' .. sid .. ' --sub-visibility=' .. sub_visibility
    .. ' --sub-delay=' .. sub_delay .. ' --sub-auto=' .. sub_auto .. ' --aid=' .. aid
    .. ' --sub-delay=' .. sub_delay .. ' --sub-auto=' .. sub_auto .. aid
    .. ' --colormatrix-input-range=' .. colormatrix_input_range
    .. ' --vf=' .. vf .. 'sub,crop=' .. crop .. ',scale=' .. scale
    .. ' --hr-seek-demuxer-offset=' .. hr_seek_demuxer_offset
  28. @Zehkul Zehkul revised this gist Dec 7, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion convert_script.lua
    Original file line number Diff line number Diff line change
    @@ -187,6 +187,7 @@ function encode ()
    video = string.gsub(video, "'", "'\\''")
    local sid = mp.get_property("sid")
    local sub_visibility = mp.get_property("sub-visibility")
    local aid = mp.get_property("aid")
    local vf = mp.get_property("vf")
    if string.len(vf) > 0 then
    vf = vf .. ","
    @@ -226,7 +227,7 @@ function encode ()

    local mpv_options = "mpv '" .. video .. "' --start=+" .. start .. ' --length=' .. length
    .. ' ' .. sub_file .. ' --sid=' .. sid .. ' --sub-visibility=' .. sub_visibility
    .. ' --sub-delay=' .. sub_delay .. ' --sub-auto=' .. sub_auto
    .. ' --sub-delay=' .. sub_delay .. ' --sub-auto=' .. sub_auto .. ' --aid=' .. aid
    .. ' --colormatrix-input-range=' .. colormatrix_input_range
    .. ' --vf=' .. vf .. 'sub,crop=' .. crop .. ',scale=' .. scale
    .. ' --hr-seek-demuxer-offset=' .. hr_seek_demuxer_offset
  29. @Zehkul Zehkul revised this gist Sep 20, 2014. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions convert_script.lua
    Original file line number Diff line number Diff line change
    @@ -21,12 +21,6 @@ local options = {

    read_options(options, "convert_script")

    if mp.get_property("playback-time") then
    playback_time = "playback-time"
    else
    playback_time = "time-pos"
    end

    -------------------------------------
    -- Rectangle selection and drawing --
    -------------------------------------
    @@ -118,6 +112,12 @@ end
    -----------------

    function convert_script_hotkey_call ()

    if mp.get_property("playback-time") then
    playback_time = "playback-time"
    else
    playback_time = "time-pos"
    end

    set_mouse_area = true
    width = mp.get_property("dwidth")
  30. @Zehkul Zehkul revised this gist Sep 20, 2014. 1 changed file with 14 additions and 7 deletions.
    21 changes: 14 additions & 7 deletions convert_script.lua
    Original file line number Diff line number Diff line change
    @@ -21,6 +21,12 @@ local options = {

    read_options(options, "convert_script")

    if mp.get_property("playback-time") then
    playback_time = "playback-time"
    else
    playback_time = "time-pos"
    end

    -------------------------------------
    -- Rectangle selection and drawing --
    -------------------------------------
    @@ -116,13 +122,12 @@ function convert_script_hotkey_call ()
    set_mouse_area = true
    width = mp.get_property("dwidth")
    height = mp.get_property("dheight")
    mp.set_mouse_area(0, 0, width, height, "draw_rectangle")
    mp.set_osd_ass(width, height, "")

    if timepos1 then

    timepos2 = mp.get_property("time-pos")
    timepos2_humanreadable = mp.get_property_osd("time-pos")
    timepos2 = mp.get_property(playback_time)
    timepos2_humanreadable = mp.get_property_osd(playback_time)

    if tonumber(timepos1) > tonumber(timepos2) then

    @@ -154,8 +159,8 @@ function convert_script_hotkey_call ()

    else

    timepos1 = mp.get_property("time-pos")
    timepos1_humanreadable = mp.get_property_osd("time-pos")
    timepos1 = mp.get_property(playback_time)
    timepos1_humanreadable = mp.get_property_osd(playback_time)
    msg.info("Start frame set")
    mp.osd_message("Start frame set")

    @@ -193,7 +198,8 @@ function encode ()
    end
    local sub_auto = mp.get_property("options/sub-auto")
    local sub_delay = mp.get_property("sub-delay")
    local colormatrix_input_range = mp.get_property_native("colormatrix-input-range")
    local colormatrix_input_range = mp.get_property_native("colormatrix-input-range")
    local hr_seek_demuxer_offset = mp.get_property_native("options/hr-seek-demuxer-offset")

    if options.use_pwd_instead then

    @@ -218,11 +224,12 @@ function encode ()

    filename = filename .. " " .. start_humanreadable .. "-" .. end_humanreadable .. ".webm"

    local mpv_options = "mpv '" .. video .. "' --start " .. start .. ' --length ' .. length
    local mpv_options = "mpv '" .. video .. "' --start=+" .. start .. ' --length=' .. length
    .. ' ' .. sub_file .. ' --sid=' .. sid .. ' --sub-visibility=' .. sub_visibility
    .. ' --sub-delay=' .. sub_delay .. ' --sub-auto=' .. sub_auto
    .. ' --colormatrix-input-range=' .. colormatrix_input_range
    .. ' --vf=' .. vf .. 'sub,crop=' .. crop .. ',scale=' .. scale
    .. ' --hr-seek-demuxer-offset=' .. hr_seek_demuxer_offset

    local encode_options = ' --oautofps --of=webm --ovc=libvpx' .. ' --ovcopts=b=' .. bitrate
    .. ',cpu-used=0,auto-alt-ref=1,lag-in-frames=25,quality=good,threads=' .. options.threads