Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
bytes object saved as .mp4
#1
I am working on a fish map solution that is a container for great info, however i am having trouble getting some media to it because when I upload video via html <form> in python3 the output is blank....however the length of the video is coded. Any Help after digesting the code?
specifically,

form = cgi.FieldStorage()
file = form.getvalue('videofile')
print (file)
with open('/var/www/fishvid/output.mp4', 'wb') as bfile:
bfile.write(file)
bfile.close()
print ('The video was successfully saved')

<url snipped>

I have a test script made that works to test..

test.py
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# enable debugging
import cgitb
cgitb.enable()
import cgi
import os

#html in python
print ('''Content-Type: text/html''') # HTML is following print 
print #12 returns in the next bracket
print ('''
<html><head>
  <title>J^2</title>
  <meta name="description" content="test" />
  <meta http-equiv="content-type" content="text/html" />
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<style>
body {background: #1339de;}
div#container
{        background: black;
 width: 50%;
  margin: 100px auto;
        color: white;
        border-radius: 1em;
        width: 1400px;
    height: 1125px;
    overflow:hidden;     /* if you don't want a scrollbar, set to hidden */
    overflow-x:hidden;   /* hides horizontal scrollbar on newer browsers */
    /* resize and min-height are optional, allows user to resize viewable area */
    -webkit-resize:vertical;
    -moz-resize:vertical;
    resize:vertical;
 //   min-height:1600px;
}
</style>
<body><div id="main"><div id="site_content"><div id="content"> <h1>Test</h1>
<form enctype='multipart/form-data' action='test.py' method='POST'><p>Location Video</p><p>Upload File: <input type = 'file' name= 'videofile' /></p><input type='submit' value='Upload' /></form> 
</div></div></div></body> </html>''')
form = cgi.FieldStorage() 
file = form.getvalue('videofile')
print (file)
with open('/var/www/fishvid/output.mp4', 'wb') as bfile:
    bfile.write(file)
    bfile.close()
print ('The video was successfully saved')
The major point is to have video upload to my webpage.
<url snipped>



fishmap.py
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# enable debugging
import cgitb
cgitb.enable()
import cgi
from datetime import date, datetime, timedelta
import csv2 as csv
import os
import base64

#html in python
print ('''Content-Type: text/html''') # HTML is following print 
print #12 returns in the next bracket
print ('''












<html><head>
  <title>J^2</title>
  <meta name="description" content="fish fishing map with Doppler Radar Map" />
  <meta http-equiv="content-type" content="text/html" />
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<style>
body {background: #1339de;}
div#container
{        background: black;
 width: 50%;
  margin: 100px auto;
        color: white;
        border-radius: 1em;
        width: 1400px;
    height: 1125px;
    overflow:hidden;     /* if you don't want a scrollbar, set to hidden */
    overflow-x:hidden;   /* hides horizontal scrollbar on newer browsers */
    /* resize and min-height are optional, allows user to resize viewable area */
    -webkit-resize:vertical;
    -moz-resize:vertical;
    resize:vertical;
 //   min-height:1600px;
}
div#containerlil
{        background: black;
 width: 50%;
  margin: 100px auto;
        color: white;
        border-radius: 1em;
        width: 320px;
    height: 240px;
    overflow:hidden;     /* if you don't want a scrollbar, set to hidden */
    overflow-x:hidden;   /* hides horizontal scrollbar on newer browsers */
    /* resize and min-height are optional, allows user to resize viewable area */
    -webkit-resize:vertical;
    -moz-resize:vertical;
    resize:vertical;
 //   min-height:640px;
}
</style>
<body><div id="main"><div id="site_content"><div id="content"> <h1>Doppler Map with Fishing Report!!!</h1>

</div></div></div></body> 
<script type="text/javascript" >
        function go() {
            var m3=document.getElementsByName("c");
            var m4=document.getElementsByName("d");
            window.location.href = "https://lftr.biz/cgi-bin/fishmap.py" + m3(0).value+"+"+ m4(0).value;
        }
       </script>
<script type="text/javascript" >
        function go2() {
            var m3=document.getElementsByName("e");
            var m4=document.getElementsByName("f");
            window.location.href = "https://lftr.biz/cgi-bin/fishmap.py" + m3(0).value+"+"+ m4(0).value;
        }
       </script>

</html>''')

print ('''
<h4>Alert to a new fishing location...!!!!</h4>
<form action="https://lftr.biz/cgi-bin/fishmap.py" onsubmit="go();">
Latitude (xxx.xxxx/-xxx.xxxx): <input type="text" name="c" value= "33.3866412"><br>
Longitude (xxx.xxxx/-xxx.xxxx): <input type="text" name="d" value= "-118.4734779"><br>
<input type="submit" value="Submit">
</form>
''')

#lat,lng,report tuple as list (commented out bc i am using a local csv file so as to WRITE reports to current locations)
#latlng = [(33.5987, -117.8826, ), (36.4147885,-118.9277509, ), (36.0765497,-118.9106086, ), (37.0396,-119.6483, ), (33.5901046, -117.870014, ), (33.5985056, -117.9033164, ), (33.6074678, -117.9309998, ), (33.6531, -118.0061, ), (33.7419998, -118.1875496, ), (34.4986553, -118.6108158, ), (34.2906126, -117.3599411, ), (34.565518, -114.3949421, ), (34.5133492, -114.3702769, ), (34.4548, -114.3755, ), (34.4495, -114.3724, ), (34.4494, -114.3712, )]

# open and read lat,lng,report tuple as list in local .csv
file = open("/var/www/cgi-bin/fishloclist.csv", "r")
csv_reader = csv.reader(file)
#make csv content a python3 list
loc = []
for row in csv_reader:
    loc.append(row)


#Reload information for subbmitting reports
#pulls the variable values from the improved url
#saves the report to the list
#try or else just load with out updating via url when the url is standard
form = cgi.FieldStorage() 
try:
    lat = float(form.getvalue("c")) 
    lng = float(form.getvalue("d")) 
    coord = (lat,lng,'')
    loc.append(coord)
    with open('/var/www/cgi-bin/fishloclist.csv', 'w', newline='') as f:
        writer = csv.writer(f)
        writer.writerows(loc)
        print ('location submited'+str(loc[-1]))

except:
    print ('')
try:
    report = str(form.getvalue("e")) 
    locnum = int(form.getvalue("f")) 
    loc[locnum].append(report)
    with open('/var/www/cgi-bin/fishloclist.csv', 'w', newline='') as f:
        writer = csv.writer(f)
        writer.writerows(loc)
        print ('report submited'+str(loc[locnum]))
except:
    print ('')
try:

    file = form.getvalue('videofile')
#    file = form[videofile]
    decodedfile = base64.b64decode(decodedfile)
    locnum = int(form.getvalue("f")) 
    os.remove('fishvid/output'+str(locnum)+'.mp4')

    with open('fishvid/output'+str(locnum)+'.mp4', 'rb') as wfile:
        wfile.write(file)
        wfile.close()
    print ('The video was successfully saved')

except:
    print ('')

###the next print section hosts ARCGIS API for JAVASCRIPT (in python)
##this happens with some integration to python variabes
#also the loop will read how many locations by length of list then pull the tuple parts and use them seperately for each point with popups is iterated on one graphics layer
print ('''<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" /><title>ArcGIS API for JavaScript 4.22</title>
<style>
      html,
      body,
      #viewDiv {
        padding: 0;
        margin: 0;
        height: 100%;
        width: 100%;      }
</style>
<link rel="stylesheet" href="https://js.arcgis.com/4.22/esri/themes/light/main.css" />
<script src="https://js.arcgis.com/4.22/"></script>
<script>require(["esri/Map", "esri/views/SceneView", "esri/layers/SceneLayer","esri/layers/WMSLayer","esri/widgets/BasemapToggle","esri/layers/TileLayer","esri/layers/VectorTileLayer","esri/layers/GraphicsLayer","esri/layers/CSVLayer","esri/widgets/Search","esri/widgets/Locate","esri/widgets/Track","esri/Graphic","esri/layers/FeatureLayer", "esri/widgets/Editor", "esri/popup/content/AttachmentsContent","esri/popup/content/TextContent"], (Map, SceneView, SceneLayer,WMSLayer,BasemapToggle,TileLayer,VectorTileLayer,GraphicsLayer,CSVLayer,Search,Locate,Track,Graphic, FeatureLayer, Editor, AttachmentsContent, TextContent) => {

const layer = new WMSLayer({url: "https://mesonet.agron.iastate.edu/cgi-bin/wms/nexrad/n0r.cgi", sublayers: [{name: "nexrad-n0r-900913"}], refreshInterval:.25});

let map = new Map({
    basemap: {baseLayers:[layer,
    new TileLayer({url: "https://services.arcgisonline.com/arcgis/rest/services/World_Terrain_Base/MapServer"}),
    new VectorTileLayer({portalItem: {id: "273bf8d5c8ac400183fc24e109d20bcf"}, blendMode: "multiply"})]},
    ground: "world-elevation",});

let view = new SceneView({container: "viewDiv", map: map, camera: {position: {x: '''+str(-117)+' , y:'+str(34)+''' , z: 12500000}, tilt: 0},});

const locate = new Locate({
          view: view,
          useHeadingEnabled: false,
          goToOverride: function(view, options) {
            options.target.scale = 1500;
            return view.goTo(options.target);
          }
        });
        view.ui.add(locate, "top-left");

const track = new Track({ view: view,graphic: new Graphic({symbol: {type: "simple-marker",size: "12px",color: "green",outline: {color: "#efefef", width: "1.5px"}}}),seHeadingEnabled: true});
        view.ui.add(track, "top-left");

const search = new Search({view: view});
view.ui.add(search, "top-right");

const toggle = new BasemapToggle({view: view, nextBasemap: "hybrid"});
view.ui.add(toggle, "top-right");
const graphicsLayer = new GraphicsLayer(); map.add(graphicsLayer );''')

length = len(loc)
for x in range(length):
    report = str(loc[x][-1])
    latitude = str(loc[x][0])
    longitude = str(loc[x][1])
####very long print statement below that makes the points w popups and takes reports
    print (''' 
var template'''+str(x)+''' =
{title: 
'Location:''' +str(x)+''' - Coordinates: ''' +latitude+' , '+longitude+' Report: '+report+ '''  ',
content:


function(){




var report = document.createElement("div"); report.className = "myClass"; report.innerHTML = "<h2>Location Video</h2><div id='containerlil'><video width='320' height='240' controls autoplay src='/fishvid/output'''+str(x)+'''.mp4' type='video/mp4'></video></div><h2>SmashUpVideo</h2><div id='containerlil'><video width='320' height='240' controls autoplay src='/fastfish.mp4'  type='video/mp4'></video></div>   <h1>Submit A New Fishing Report</h1><form action='https://lftr.biz/cgi-bin/fishmap.py' onsubmit='go2();'>Enter Report: <input type='text' name='e' value= 'date, observation, tips & tricks'><br><input name ='f' value= '''+str(x)+'''  type='hidden'><input type='submit' value='Submit'> </form>  <form enctype='multipart/form-data' action='fishmap.py?f= '''+str(x)+''' ' method='POST'><p>Location Video</p><p>Upload File: <input type = 'file' name= 'videofile' /></p><input type='submit' value='Upload' /></form>   "; return report;
}
};

const point'''+str(x)+''' = {type: "point", x: ''' +longitude+ ''' , y: ''' +latitude+ ''' , z: 4010}; 
const markerSymbol'''+str(x)+''' = {type: "simple-marker", color: [226, 119, 40], outline: {color: [255, 255, 255], width: 4}}; 
const pointGraphic'''+str(x)+''' = new Graphic({geometry: point'''+str(x)+''' , popupTemplate: template'''+str(x)+''' , symbol: markerSymbol'''+str(x)+'''}); 
graphicsLayer.add(pointGraphic'''+str(x)+''' ); 

''')
print (''' }); </script></head><body><div id="container"><div id="viewDiv"></div></div></body></html>''')
#############################################################################
Gribouillis write Jan-24-2022, 08:02 AM:
Url snipped because it caused a security warning from my browser. Please read the help section about what not to include in a post.
Reply
#2
I have tried multiple solutions to find that all have a codec error on the input stream and I have h264 on the Google Ubuntu server...so I have also updated the upload test, which is to update the fishmap with video.

Test2.py
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# enable debugging
import cgitb
cgitb.enable()
import cgi
import os
import base64
import cv2
import subprocess
import json
import ffmpeg

#html in python
print ('''Content-Type: text/html''') # HTML is following print 
print #12 returns in the next bracket
print ('''













<html><head>
  <title>J^2</title>
  <meta name="description" content="test" />
  <meta http-equiv="content-type" content="text/html" />
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<style>
body {background: #1339de;}
div#container
{        background: black;
 width: 50%;
  margin: 100px auto;
        color: white;
        border-radius: 1em;
        width: 1400px;
    height: 1125px;
    overflow:hidden;     /* if you don't want a scrollbar, set to hidden */
    overflow-x:hidden;   /* hides horizontal scrollbar on newer browsers */
    /* resize and min-height are optional, allows user to resize viewable area */
    -webkit-resize:vertical;
    -moz-resize:vertical;
    resize:vertical;
 //   min-height:1600px;
}
</style>
<body><div id="main"><div id="site_content"><div id="content"> <h1>Test</h1>


<html>
<body>

<form enctype = "multipart/form-data" action = "test2.py" method = "post">
<p>Upload File: <input type = "file" name = "filename" /></p>
<p><input type = "submit" value = "Upload" /></p>


</body>
</html>


</div></div></div></body> </html>''')
try:
    command = ('cd /var/www/fishvid && sudo rm temp.mp4')
    subprocess.call(command, shell=True, stderr=subprocess.STDOUT, stdin=subprocess.DEVNULL)
    command = ('cd /var/www/fishvid && sudo rm out.mp4')
    subprocess.call(command, shell=True, stderr=subprocess.STDOUT, stdin=subprocess.DEVNULL)
except:
    print ('complete')


form = cgi.FieldStorage()
fileitem = form['filename']
# check if the file has been uploaded
if fileitem.filename:
    # strip the leading path from the file name    fn = os.path.basename(fileitem.filename)
   # open read and write the file into the server
    
    
    open('/var/www/fishvid/temp.mp4', 'wb').write(fileitem.file.read()) 
path = '/var/www/fishvid/temp.mp4' 
cap = cv2.VideoCapture('/var/www/fishvid/temp.mp4') 
cap.set(3,640) 
cap.set(4,480) 
fourcc = cv2.VideoWriter_fourcc(*'MP4V') 
out = cv2.VideoWriter('/var/www/fishvid/outputnewnew.mp4', fourcc, 20.0, (640,480))



fps = int(cap.get(cv2.CAP_PROP_FPS)) 
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) 
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
print (height, width)
# command and params for ffmpeg
command = ['ffmpeg', '-y', '-f', 
           'rawvideo', '-vcodec', 
           'rawvideo', 
           '-pix_fmt', 'yuv420p', 
           '-s', 
           "{}x{}".format(width, 
           height), '-r', 
           str(fps), '-i', '-', 
           '-c:v', 'libx264', 
           '-pix_fmt', 'yuv420p', 
           '-preset', 
           'ultrafast', '-f', 
           'mp4', path]
subprocess.call(command, shell=True, stderr=subprocess.STDOUT, stdin=subprocess.DEVNULL)
What does anyone 🤔
Reply
#3
Like I say...I tried many solutions here is another more complex solution with a readout.

 #!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# enable debugging
import cgitb
cgitb.enable()
import cgi
import os
import base64
import cv2
import subprocess
import json
import ffmpeg
from ffmpeg_reader import FFMPEG_VideoReader
#html in python
print ('''Content-Type: text/html''') # HTML is following print 
print #12 returns in the next bracket
print ('''













<html><head>
  <title>J^2</title>
  <meta name="description" content="test" />
  <meta http-equiv="content-type" content="text/html" />
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<style>
body {background: #1339de;}
div#container
{        background: black;
 width: 50%;
  margin: 100px auto;
        color: white;
        border-radius: 1em;
        width: 1400px;
    height: 1125px;
    overflow:hidden;     /* if you don't want a scrollbar, set to hidden */
    overflow-x:hidden;   /* hides horizontal scrollbar on newer browsers */
    /* resize and min-height are optional, allows user to resize viewable area */
    -webkit-resize:vertical;
    -moz-resize:vertical;
    resize:vertical;
 //   min-height:1600px;
}
</style>
<body><div id="main"><div id="site_content"><div id="content"> <h1>Test</h1>


<html>
<body>

<form enctype = "multipart/form-data" action = "test.py" method = "post">
<p>Upload File: <input type = "file" name = "filename" /></p>
<p><input type = "submit" value = "Upload" /></p>


</body>
</html>


</div></div></div></body> </html>''')
try:
    command = ('cd /var/www/fishvid && sudo rm temp.mp4')
    subprocess.call(command, shell=True, stderr=subprocess.STDOUT, stdin=subprocess.DEVNULL)
    command = ('cd /var/www/fishvid && sudo rm out.mp4')
    subprocess.call(command, shell=True, stderr=subprocess.STDOUT, stdin=subprocess.DEVNULL)
except:
    print ('complete')


form = cgi.FieldStorage()
bmp4 = form['filename']
# check if the file has been uploaded
if bmp4.filename:
    open('/var/www/fishvid/temp.mp4', 'wb').write(bmp4.file.read()) 

FFMPEG_VideoReader('/var/www/fishvid/temp.mp4')  
    Test
Upload File: No file chosen


rm: cannot remove 'temp.mp4': No such file or directory rm: cannot remove 'out.mp4': No such file or directory --> -->
Traceback (most recent call last):
  File "/var/www/cgi-bin/ffmpeg_reader.py", line 818, in ffmpeg_parse_infos
    decode_file=decode_file,
  File "/var/www/cgi-bin/ffmpeg_reader.py", line 556, in parse
    if len(input_chapters) == self._current_input_file["input_number"] + 1:
KeyError: 'input_number'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/var/www/cgi-bin/test.py", line 87, in <module>
    FFMPEG_VideoReader('/var/www/fishvid/temp.mp4')
  File "/var/www/cgi-bin/ffmpeg_reader.py", line 36, in __init__
    print_infos=print_infos,
  File "/var/www/cgi-bin/ffmpeg_reader.py", line 825, in ffmpeg_parse_infos
    raise IOError(f"Error passing `ffmpeg -i` command output:\n\n{infos}") from exc
OSError: Error passing `ffmpeg -i` command output:

[mov,mp4,m4a,3gp,3g2,mj2 @ 0x55ca3a507300] Could not find codec parameters for stream 0 (Video: h264 (avc1 / 0x31637661), none(tv, bt709), 1280x720, 9571 kb/s): unspecified pixel format
Consider increasing the value for the 'analyzeduration' (0) and 'probesize' (5000000) options
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/var/www/fishvid/temp.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    creation_time   : 2022-02-16T20:04:14.000000Z
    location        : +36.3243-119.3011/
    location-eng    : +36.3243-119.3011/
    com.android.version: 11
  Duration: 00:00:01.52, start: 0.002700, bitrate: 85 kb/s
  Stream #0:0[0x1](eng): Video: h264 (avc1 / 0x31637661), none(tv, bt709), 1280x720, 9571 kb/s, SAR 1:1 DAR 16:9, 29.61 fps, 29.61 tbr, 90k tbn (default)
    Metadata:
      creation_time   : 2022-02-16T20:04:14.000000Z
      handler_name    : VideoHandle
      vendor_id       : [0][0][0][0]
    Side data:
      displaymatrix: rotation of -90.00 degrees
  Stream #0:1[0x2](eng): Audio: aac (mp4a / 0x6134706D), 48000 Hz, mono, fltp, 128 kb/s (default)
    Metadata:
      creation_time   : 2022-02-16T20:04:14.000000Z
      handler_name    : SoundHandle
      vendor_id       : [0][0][0][0]
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> wrapped_avframe (native))
  Stream #0:1 -> #0:1 (aac (native) -> pcm_s16le (native))
Press [q] to stop, [?] for help
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x55ca3a507300] stream 1, offset 0x6301b: partial file
Cannot determine format of input stream 0:0 after EOF
Error marking filters as finished
Conversion failed!
 
.



This also has a codec issue.... I guess it's an ffmpeg coding issue......any thoughts or help?
Reply
#4
hello everyone, i have done some more research and followed a tutorial on uploading to a server in python cgi land:

GeeksForGeeks.com

https://www.geeksforgeeks.org/upload-files-in-python/


#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# enable debugging
import cgitb
cgitb.enable()
import cgi
import os
 
#html in python
print ('''Content-Type: text/html''') # HTML is following print 
print #12 returns in the next bracket
print ('''













<html><head>
  <title>J^2</title>
  <meta name="description" content="test" />
  <meta http-equiv="content-type" content="text/html" />
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<style>
body {background: #1339de;}
div#container
{        background: black;
 width: 50%;
  margin: 100px auto;
        color: white;
        border-radius: 1em;
        width: 1400px;
    height: 1125px;
    overflow:hidden;     /* if you don't want a scrollbar, set to hidden */
    overflow-x:hidden;   /* hides horizontal scrollbar on newer browsers */
    /* resize and min-height are optional, allows user to resize viewable area */
    -webkit-resize:vertical;
    -moz-resize:vertical;
    resize:vertical;
 //   min-height:1600px;
}
</style>
<body><div id="main"><div id="site_content"><div id="content"> <h1>Test</h1>


<form enctype = "multipart/form-data" action = "test.py" method = "post">
<p>Upload File: <input type = "file" name = "filename" /></p>
<p><input type = "submit" value = "Upload" /></p> </form>


</div></div></div></body> </html>''')


fileitem = form['filename']
 
# check if the file has been uploaded
if fileitem.filename:
    # strip the leading path from the file name
    fn = os.path.basename(fileitem.filename)
     
   # open read and write the file into the server
    open(fn, 'wb').write(fileitem.file.read())
the file will upload to the server....

after going back i have success, however the file still will not play, the video has infos is correct and has time, it is just blank

So, I try to, well; chmod 777 and a+x even -R for all: without improvement.

so to recap this will load on a webpage browsable by my cell phone ask for the camcorder in html, save a .mp4, send it and save it again on my server.........on the web.....in binary....however the server video wont play in its directory when browsed to on the web again....so i cannot view the upload....it is blank.......

very cool how close i really am to my goal and this is a small stumbling block ......

so it is taking some time.

Does any one know why my GeeeksForGeeks.com Tutorial uploads mostly a video I cannot play?

[attachment=1614][attachment=1613][attachment=1615]
Reply
#5
When use CGI(dead in Python),you will probably not get any help.
Python has moved away from CGI and written and all Python solution PEP 3333 | WSGI to deal with modern web development.
So today is all Python framework(eg Flask, Django) build on(WSGI) in bottom.
CGI will be removed from standard library in Python 3.11.
ndc85430 likes this post
Reply
#6
(Feb-20-2022, 05:33 PM)snippsat Wrote: When use CGI(dead in Python),you will probably not get any help.
Python has moved away from CGI and written and all Python solution PEP 3333 | WSGI to deal with modern web development.
So today is all Python framework(eg Flask, Django) build on(WSGI) in bottom.
CGI will be removed from standard library in Python 3.11.

I believe I'm with a very current python3 above 3.11 and cgi is used by the webserver....in python3 to support a python program that is not working, totally. I can achieve half the upload with the upload script above, when run, however the file has a blank video screen, i really need a python3 solution. soon.

I have used flask and django too, they could be solutions?[attachment=1617][attachment=1616]
Reply
#7
well, my best hunch is that I have a header issue, many of the examples; like requests, include mime type 'video/mp4' in the headers. With the cgi form data i am able to upload a bytes file that is written to the drive, however it is a blank 21 second or so video and this may be why, incorrect headers.....well, my best hunch is that I have a header issue, many of the examples; like requests, include mime type 'video/mp4' in the headers. With the cgi form data i am able to upload a bytes file that is written to the drive, however it is a blank 21 second or so video and this may be why, incorrect headers.....

here is my updated script in python3:

test.py
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# enable debugging
import cgitb
cgitb.enable()
import cgi
import os

#html in python
print ('''Content-Type: text/html\n''')
print ('''
<html><head>
  <title>J^2</title>
  <meta name="description" content="test" />
  <meta http-equiv="content-type" content="text/html" />
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<style>
body {background: #1339de;}
</style>
<html><body>
<form action="test.py" method="POST" enctype="multipart/form-data">
  <input type="file" name="file"></input>
  <button type="submit" name="submit" id='submit'>SUBMIT</button>
</form>
</body></html>''')
def fbuffer(f, chunk_size=10000):
    while True:
        chunk = f.read(chunk_size)
        if not chunk: break
        yield chunk
form = cgi.FieldStorage()
fileitem = form['file']
print (fileitem)
num = int(777)
name = 'output'+str(num)+'.mp4'
if fileitem.filename:
    fn = os.path.basename(fileitem.filename)
    f = open('/var/www/fishvid/'+str(name), 'wb', 10000)
    for chunk in fbuffer(fileitem.file):
        f.write(chunk)
    f.close()
    messege = 'The file "' + fn + '" was uploaded successfully'
else:
    messege = 'No file was uploaded'
print ('<h1>'+str(messege)+'</h1>')
Reply
#8
I found some code that may apply for future topic, here, the headers can be changed in my current code here:

form = cgi.FieldStorage()
by inserting header in the parenthesis:

form = cgi.FieldStorage('headers here')
using this code as an example: could we set the mime type this way?
			form = cgi.FieldStorage(fp=, headers=, environ= )
Reply
#9
A working example with Flask that you can look at,or if you can use it's a better way.
So make in one file(not the ideal way) and just copy your html and make one change.
Tested with a 2-Gb .mp4 file and it work fine.
from flask import Flask, render_template, request
from werkzeug.utils import secure_filename

app = Flask(__name__)
@app.route("/upload")
def upload():
    return """\
    <html>
    <head>
      <title>J^2</title>
      <meta name="description" content="test" />
      <meta http-equiv="content-type" content="text/html" />
      <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <style>
    body {
        background: #1339de;
    }
    </style>  
    <body>
    <form  action="http://localhost:5000/uploader" method="POST" enctype="multipart/form-data">
        <input type="file" name="file"></input>
        <button type="submit" name="submit" id='submit'>SUBMIT</button>
    </form>
    </body>
    </html>"""

@app.route("/uploader", methods=["GET", "POST"])
def upload_file():
    if request.method == "POST":
        f = request.files["file"]
        f.save(secure_filename(f.filename))
        return "file uploaded successfully"

if __name__ == "__main__":
    app.run(debug=True)
jttolleson likes this post
Reply
#10
(Feb-24-2022, 09:31 PM)snippsat Wrote: A working example with Flask that you can look at,or if you can use it's a better way.
So make in one file(not the ideal way) and just copy your html and make one change.
Tested with a 2-Gb .mp4 file and it work fine.
from flask import Flask, render_template, request
from werkzeug.utils import secure_filename

app = Flask(__name__)
@app.route("/upload")
def upload():
    return """\
    <html>
    <head>
      <title>J^2</title>
      <meta name="description" content="test" />
      <meta http-equiv="content-type" content="text/html" />
      <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <style>
    body {
        background: #1339de;
    }
    </style>  
    <body>
    <form  action="http://localhost:5000/uploader" method="POST" enctype="multipart/form-data">
        <input type="file" name="file"></input>
        <button type="submit" name="submit" id='submit'>SUBMIT</button>
    </form>
    </body>
    </html>"""

@app.route("/uploader", methods=["GET", "POST"])
def upload_file():
    if request.method == "POST":
        f = request.files["file"]
        f.save(secure_filename(f.filename))
        return "file uploaded successfully"

if __name__ == "__main__":
    app.run(debug=True)

Smile so helpful, so i guess it is a flask solution.
I have a version working...some it uploads to my root of my server with the the filename it set out with working.

however, I would like to get it to a different file path soon with a generated name.....so

this is working

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# enable debugging
import cgitb
cgitb.enable()
import cgi
from flask import Flask, render_template, request
from werkzeug.utils import secure_filename

form = cgi.FieldStorage() 
locnum = int(form.getvalue("f")) 
print ('video for location 'locnum)

app = Flask(__name__)
@app.route("/uploader", methods=["GET", "POST"])
def upload_file():
    if request.method == "POST":
        f = request.files["file"]
        f.save(secure_filename(f.filename))
#        f.save('/var/www/fishvid/temp'+str(locnum)+'.mp4')
        return "file uploaded successfully"
 
if __name__ == "__main__":
    app.run(host='0.0.0.0')
i may change the host to the fishvid folder somehow? however, once again to name the file with the location number....

i call post the data the original way i had, with an html form in fishmap.py

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# enable debugging
import cgitb
cgitb.enable()
import cgi
from datetime import date, datetime, timedelta
import csv
import os

#html in python
print ('''Content-Type: text/html\n''')
print ('''
<html><head>
  <title>J^2</title>
  <meta name="description" content="fish fishing map with Doppler Radar Map" />
  <meta http-equiv="content-type" content="text/html" />
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<style>
body {background: #1339de;}
div#container
{        background: black;
 width: 50%;
  margin: 100px auto;
        color: white;
        border-radius: 1em;
        width: 1400px;
    height: 1125px;
    overflow:hidden;     /* if you don't want a scrollbar, set to hidden */
    overflow-x:hidden;   /* hides horizontal scrollbar on newer browsers */
    /* resize and min-height are optional, allows user to resize viewable area */
    -webkit-resize:vertical;
    -moz-resize:vertical;
    resize:vertical;
 //   min-height:1600px;
}
div#containerlil
{        background: black;
 width: 50%;
  margin: 100px auto;
        color: white;
        border-radius: 1em;
        width: 320px;
    height: 240px;
    overflow:hidden;     /* if you don't want a scrollbar, set to hidden */
    overflow-x:hidden;   /* hides horizontal scrollbar on newer browsers */
    /* resize and min-height are optional, allows user to resize viewable area */
    -webkit-resize:vertical;
    -moz-resize:vertical;
    resize:vertical;
 //   min-height:640px;
}
iframe#embedlil {
    width:320px; /* set this to approximate width of entire page you're embedding */
    height:240px; /* determines where the bottom of the page cuts off */
    margin-left:0px; /* clipping left side of page */
    margin-top:0px; /* clipping top of page */
    overflow:hidden;
    /* resize seems to inherit in at least Firefox */
    -webkit-resize:none;
    -moz-resize:none;
    resize:none;
}
</style>
<body><div id="main"><div id="site_content"><div id="content"> <h1>Doppler Map with Fishing Report!!!</h1>

</div></div></div></body> 
<script type="text/javascript" >
        function go() {
            var m3=document.getElementsByName("c");
            var m4=document.getElementsByName("d");
            window.location.href = "https://lftr.biz/cgi-bin/fishmap.py" + m3(0).value+"+"+ m4(0).value;
        }
       </script>
<script type="text/javascript" >
        function go2() {
            var m3=document.getElementsByName("e");
            var m4=document.getElementsByName("f");
            window.location.href = "https://lftr.biz/cgi-bin/fishmap.py" + m3(0).value+"+"+ m4(0).value;
        }
       </script>

</html>''')

print ('''
<h4>Alert to a new fishing location...!!!!</h4>
<form action="https://lftr.biz/cgi-bin/fishmap.py" onsubmit="go();">
Latitude (xxx.xxxx/-xxx.xxxx): <input type="text" name="c" value= "33.3866412"><br>
Longitude (xxx.xxxx/-xxx.xxxx): <input type="text" name="d" value= "-118.4734779"><br>
<input type="submit" value="Submit">
</form>
''')

#lat,lng,report tuple as list (commented out bc i am using a local csv file so as to WRITE reports to current locations)
#latlng = [(33.5987, -117.8826, ), (36.4147885,-118.9277509, ), (36.0765497,-118.9106086, ), (37.0396,-119.6483, ), (33.5901046, -117.870014, ), (33.5985056, -117.9033164, ), (33.6074678, -117.9309998, ), (33.6531, -118.0061, ), (33.7419998, -118.1875496, ), (34.4986553, -118.6108158, ), (34.2906126, -117.3599411, ), (34.565518, -114.3949421, ), (34.5133492, -114.3702769, ), (34.4548, -114.3755, ), (34.4495, -114.3724, ), (34.4494, -114.3712, )]

# open and read lat,lng,report tuple as list in local .csv
file = open("/var/www/cgi-bin/fishloclist.csv", "r")
csv_reader = csv.reader(file)
#make csv content a python3 list
loc = []
for row in csv_reader:
    loc.append(row)


#Reload information for subbmitting reports
#pulls the variable values from the improved url
#saves the report to the list
#try or else just load with out updating via url when the url is standard
form = cgi.FieldStorage() 
try:
    lat = float(form.getvalue("c")) 
    lng = float(form.getvalue("d")) 
    coord = (lat,lng,'')
    loc.append(coord)
    with open('/var/www/cgi-bin/fishloclist.csv', 'w', newline='') as f:
        writer = csv.writer(f)
        writer.writerows(loc)
        print ('location submited'+str(loc[-1]))

except:
    print ('')
try:
    report = str(form.getvalue("e")) 
    locnum = int(form.getvalue("f")) 
    loc[locnum].append(report)
    with open('/var/www/cgi-bin/fishloclist.csv', 'w', newline='') as f:
        writer = csv.writer(f)
        writer.writerows(loc)
        print ('report submited'+str(loc[locnum]))
except:
    print ('')
try:

    file = form.getvalue('videofile')
#    decodedfile = base64.b64decode(decodedfile)
    locnum = int(form.getvalue("f")) 
    os.remove('fishvid/output'+str(locnum)+'.mp4')

    with open('fishvid/output'+str(locnum)+'.mp4', 'wb') as bfile:
        bfile.write(file)
        bfile.close()
    print ('The video was successfully saved')

except:
    print ('')

###the next print section hosts ARCGIS API for JAVASCRIPT (in python)
##this happens with some integration to python variabes
#also the loop will read how many locations by length of list then pull the tuple parts and use them seperately for each point with popups is iterated on one graphics layer

#time variables
import datetime
from datetime import date, datetime, timedelta
import datetime as dt
import time
day3 = dt.date.today()-dt.timedelta(days=3)
tend = time.strftime('%Y'+'%m'+'%d')
tminus = str(time.strftime('%Y'+'%m'))+str(int(time.strftime('%d'))-1)
print ('''<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" /><title>ArcGIS API for JavaScript 4.22</title>
<style>
      html,
      body,
      #viewDiv {
        padding: 0;
        margin: 0;
        height: 100%;
        width: 100%;      }
</style>
<link rel="stylesheet" href="https://js.arcgis.com/4.22/esri/themes/light/main.css" />
<script src="https://js.arcgis.com/4.22/"></script>
<script>require(["esri/Map", "esri/views/SceneView", "esri/layers/SceneLayer","esri/layers/WMSLayer","esri/widgets/BasemapToggle","esri/layers/TileLayer","esri/layers/VectorTileLayer","esri/layers/GraphicsLayer","esri/layers/CSVLayer","esri/widgets/Search","esri/widgets/Locate","esri/widgets/Track","esri/Graphic","esri/layers/FeatureLayer", "esri/widgets/Editor", "esri/popup/content/AttachmentsContent","esri/popup/content/TextContent"], (Map, SceneView, SceneLayer,WMSLayer,BasemapToggle,TileLayer,VectorTileLayer,GraphicsLayer,CSVLayer,Search,Locate,Track,Graphic, FeatureLayer, Editor, AttachmentsContent, TextContent) => {

const layer = new WMSLayer({url: "https://mesonet.agron.iastate.edu/cgi-bin/wms/nexrad/n0r.cgi", sublayers: [{name: "nexrad-n0r-900913"}], refreshInterval:.25});

let map = new Map({
    basemap: {baseLayers:[layer,
    new TileLayer({url: 'https://services.arcgisonline.com/arcgis/rest/services/World_Imagery/MapServer/?SRC_DATE= '''+str(tminus)+''' &SRC_DATE2= '''+str(tend)+''' '}),     ]},
    ground: "world-elevation",});

let view = new SceneView({container: "viewDiv", map: map, camera: {position: {x: '''+str(-118)+' , y:'+str(34)+''' , z: 12500000}, tilt: 0},});

const locate = new Locate({
          view: view,
          useHeadingEnabled: false,
          goToOverride: function(view, options) {
            options.target.scale = 1500;
            return view.goTo(options.target);
          }
        });
        view.ui.add(locate, "top-left");

const track = new Track({ view: view,graphic: new Graphic({symbol: {type: "simple-marker",size: "12px",color: "green",outline: {color: "#efefef", width: "1.5px"}}}),seHeadingEnabled: true});
        view.ui.add(track, "top-left");

const search = new Search({view: view});
view.ui.add(search, "top-right");

const toggle = new BasemapToggle({view: view, nextBasemap: "hybrid"});
view.ui.add(toggle, "top-right");
const graphicsLayer = new GraphicsLayer(); map.add(graphicsLayer );''')

length = len(loc)
for x in range(length):
    report = str(loc[x][-1])
    latitude = str(loc[x][0])
    longitude = str(loc[x][1])
####very long print statement below that makes the points w popups and takes reports
    print (''' 
var template'''+str(x)+''' =
{title: 
'Location:''' +str(x)+''' - Coordinates: ''' +latitude+' , '+longitude+' Report: '+report+ '''  ',
content:


function(){




var report = document.createElement("div"); report.className = "myClass"; report.innerHTML = "<h2>Location Video</h2><div id='containerlil'><video width='320' height='240' controls autoplay src='/fishvid/output'''+str(x)+'''.mp4' type='video/mp4'></video></div><h2>SmashUpVideo</h2><div id='containerlil'><video width='320' height='240' controls autoplay src='/fastfish.mp4'  type='video/mp4'></video></div>   <h1>Submit A New Fishing Report</h1><form action='https://lftr.biz/cgi-bin/fishmap.py' onsubmit='go2();'>Enter Report: <input type='text' name='e' value= 'date, observation, tips & tricks'><br><input name ='f' value= '''+str(x)+'''  type='hidden'><input type='submit' value='Submit'> </form><form enctype = 'multipart/form-data' action = 'http://lftr.biz:5000/uploader' method = 'post'><p>Upload File For The Location, ie whats up on vid....: <input type = 'file' name = 'file' /><input name ='f' value= '''+str(x)+'''  type='hidden'></p><p><input type = 'submit' value = 'Upload' /></p> "; return report;
}
};

const point'''+str(x)+''' = {type: "point", x: ''' +longitude+ ''' , y: ''' +latitude+ ''' , z: 4010}; 
const markerSymbol'''+str(x)+''' = {type: "simple-marker", color: [226, 119, 40], outline: {color: [255, 255, 255], width: 4}}; 
const pointGraphic'''+str(x)+''' = new Graphic({geometry: point'''+str(x)+''' , popupTemplate: template'''+str(x)+''' , symbol: markerSymbol'''+str(x)+'''}); 
graphicsLayer.add(pointGraphic'''+str(x)+''' ); 

''')
print (''' }); </script></head><body><div id="container"><div id="viewDiv"></div></div></body></html>''')
#############################################################################
thank you - for your assistance in uploading, I still must figure out how to include the locnum in the form and filename...
[attachment=1626]
[attachment=1623][attachment=1624][attachment=1625]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: a bytes-like object is required ZeroX 13 4,077 Jan-07-2023, 07:02 PM
Last Post: deanhystad
  TypeError: a bytes-like object is required, not 'str' - Help Please. IanJ 3 4,792 Aug-29-2022, 05:53 PM
Last Post: deanhystad
  Using .pb saved model for object detection hobbyist 2 1,170 Aug-03-2022, 05:55 AM
Last Post: hobbyist
  python 3: TypeError: a bytes-like object is required, not 'str' wardancer84 3 6,475 Jul-09-2021, 05:55 PM
Last Post: deanhystad
  TypeError: int() argument must be a string, a bytes-like object or a number, not 'Non Anldra12 2 5,190 May-02-2021, 03:45 PM
Last Post: Anldra12
  Why can't numpy array be restored to a saved value? pjfarley3 1 1,708 Nov-25-2020, 07:40 AM
Last Post: pjfarley3
  Only getting last record saved...Why Milfredo 10 4,357 Sep-10-2020, 03:00 AM
Last Post: Milfredo
  Running scripts and location of saved interpreted user-defined classes and functions leodavinci1990 3 2,514 Aug-25-2020, 03:43 AM
Last Post: micseydel
  how to solve "a bytes-like object is required, not 'str'" error maiya 2 3,782 Jul-28-2020, 07:03 PM
Last Post: bowlofred
  TypeError: a bytes-like object is required, not 'str' ozzy69 1 2,850 Jul-17-2020, 03:38 PM
Last Post: stullis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020